OK, I've done it like that. But the issue is still there.
This is my Model (User)
<?php
namespace MyApp\Models;
use Phalcon\Mvc\Model\Validator\Email as Email;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness;
class User extends \Phalcon\Mvc\Model
{
/**
*
* @var string
* @Primary
* @Identity
* @Column(type="string", length=20, nullable=false)
*/
public $id;
/**
*
* @var string
* @Column(type="string", length=100, nullable=false)
*/
public $email;
/**
* Initialize method for model.
*/
public function initialize()
{
$this->hasMany('id', __NAMESPACE__ . '\UserMeta', 'user_id', ['alias' => 'userMeta']]);
}
/**
* Returns table name mapped in the model.
*
* @return string
*/
public function getSource()
{
return 'user';
}
/**
* Allows to query a set of records that match the specified conditions
*
* @param mixed $parameters
* @return User[]
*/
public static function find($parameters = null)
{
return parent::find($parameters);
}
/**
* Allows to query the first record that match the specified conditions
*
* @param mixed $parameters
* @return User
*/
public static function findFirst($parameters = null)
{
return parent::findFirst($parameters);
}
/**
* Validate that emails/usernames are unique across users
*/
public function validation()
{
$validator = new Validation();
$validator->add('email', new Uniqueness([
"message" => "Este email ya está registrado"
]));
return $this->validate($validator);
}
/**
* Return the metadata value for the key
*
* @param $key
* @return string
*/
public function getMeta($key)
{
if($this->getUserMeta(["key = '" . $key . "'"])->count())
return $this->getUserMeta(["key = '" . $key . "'"])->getFirst()->value;
else
return false;
}
/**
* Set the metadata value for the key
*
* @param $key
* @param $value
*/
public function setMeta($key, $value)
{
$userMeta = new UserMeta();
// Comprobamos si existe ese Meta con ese Key en la base de datos
if($current = $userMeta->findFirst(["user_id=$this->id and key='$key'"]))
// Actuamos sobre el meta que ya existe
$userMeta->id = $current->id;
// Si pasamos un valor, lo guardamos
if(!empty($value)) {
$userMeta->user_id = $this->id;
$userMeta->key = $key;
$userMeta->value = $value;
if($userMeta->save()===false) {
foreach ($userMeta->getMessages() as $message) {
$this->getDI()
->getFlashSession()
->error($message);
}
return false;
}
return true;
}
// Si no pasamos un valor, borramos el meta
else {
if($current) {
if($userMeta->delete()===false) {
foreach ($userMeta->getMessages() as $message) {
$this->getDI()
->getFlashSession()
->error($message);
}
return false;
}
}
return true;
}
}
public function getName() {
return $this->getMeta("name");
}
public function setName($value) {
$this->setMeta("name", $value);
}
}