Hello, I'm trying to get the 'value' of a 'key' corresponding to a Metadata of a User but I can't find the way of doing it.
I have two models, User and UserMeta:
namespace MyApp\Models;
class User extends \Phalcon\Mvc\Model
{
public $id;
public $email;
public function initialize()
{
$this->hasMany('id', __NAMESPACE__ . '\UserMeta', 'user_id', ['alias' => 'userMeta']);
}
}
namespace MyApp\Models;
class UserMeta extends \Phalcon\Mvc\Model
{
public $id;
public $user_id;
public $key;
public $value;
public function initialize()
{
$this->belongsTo('user_id', __NAMESPACE__ . '\User', 'id', ['alias' => 'user']);
}
}
I have these tables:
User table
+----+---------------+
| id | email |
+----+---------------+
| 1 | [email protected] |
+----+---------------+
UserMeta table
+----+------------+----------------+--------------+
| id | user_id | key | value |
+----+------------+----------------+--------------+
| 1 | 1 | first_name | Smith |
+----+------------+----------------+--------------+
What I'm trying to do is to access the value for the 'first_name' key in the UserMeta Model.
I mean, I'm trying to get the First Name of user '[email protected]' -> "Smith"
I'm doing it in this way:
$email = "[email protected]";
$user = User::findFirstByEmail($email);
$firstName = $user->getUserMeta(["key" => "first_name"])->value;
But I can't get results
Does somebody know what is the best way of doing this?