I have been trying to figure out this issue for hours. For some reason I'm getting the following exception:
Phalcon\Mvc\Model\Exception: There are no defined relations for the model "eav\EavObjectValue" using alias "field"
There are 3 models involved - EavField (record already created before), EavObject (record created before within the same request) and EavObjectValue (has foreign key references to the other 2 tables, INSERT fails).
Here's the code excerpt with the non-relevant pieces removed:
class EavObjectValue extends Model
{
public $ID;
public $objectID;
public $fieldID;
public $numValue;
public $stringValue;
public $textValue;
public $dateValue;
public function initialize()
{
$this->belongsTo('objectID', 'eav\EavObject', 'ID', array('alias' => 'EavObject'));
$this->belongsTo('fieldID', 'eav\EavField', 'ID', array('alias' => 'EavField'));
}
public function beforeCreate()
{
// checking that the referenced records exist in database
var_dump($this->getEavObject());
var_dump($this->getEavField());
}
...
class EavObject extends Model
{
public $ID;
public function initialize()
{
$this->hasMany('ID', 'eav\EavObjectValue', 'objectID', array(
'alias' => 'EavObjectValue',
'foreignKey' => array(
'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
)
));
}
...
class EavField extends Model
{
public $ID;
public function initialize()
{
$this->hasMany('ID', 'eav\EavObjectValue', 'fieldID', array(
'alias' => 'EavObjectValue',
'foreignKey' => array(
'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
)
));
}
Here's the generated INSERT query:
2250 Query DESCRIBE `EavObjectValue`
2250 Query INSERT INTO `EavObjectValue` (`objectID`, `fieldID`, `numValue`, `stringValue`, `textValue`, `dateValue`) VALUES ('119', '26', null, null, null, '2013-10-25T21:00:00.000Z')
2250 Query rollback
I'm certain that the EavObject exists because it's also referenced in other models for which the records are created within the same operation. When I try to execute the SQL manually, it works if I replace the objectID value with one already existing in the database, so it shouldn't be a foreign key related error coming from the database.
I don't really see what am I doing wrong. And the error message is strange, because there's no alias "field" defined, so I'm not sure where that comes from (fieldID column?). I would appreciate any suggestions.