We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Models relationship

How can i get video from BlogsPostsMeta, and check if its true or not? Note, this is BlogsPosts that is in a relationsship with BlogsPostsMeta.

*Unknown model or alias 'meta' (11), when preparing: SELECT [Models\BlogsPosts]. FROM [Models\BlogsPosts] WHERE meta.video = ?1 ORDER BY id DESC LIMIT :APL0: OFFSET :APL1:**

public function initialize()
{
    $this->belongsTo('id', __NAMESPACE__ . '\BlogsPostsMeta', 'postid', array(
        'alias' => 'meta'
    ));

}
$posts = BlogsPosts::find(
    array(
        'conditions' => 'meta.video = ?1',
        'bind'       => array(1 => '1'),
        'limit'      => $limit,
        'offset'     => $offset, 
        'order'      => 'id DESC'
        )
    );

Not entirely sure, but the alias might need to be the actual entity class, so in this case BlogsPostsMeta, don't know if alias works like use Somenamespace\Classname as Foo.

Sorry, dosent work :-(

edited Apr '16

You need to join it. Use modelsManager or BlogsPosts::query()

But isent that bad, when I already use relationships ?

edited Apr '16

It doesn't matter. find/findFirst method is only for finding records in current object related matter. It doesn't join related records. You need to use modelsManager/::query in this case.

Still you can use relations in modelsManager/::query - with them you don't need to type condition.

meta is only alias to access meta when iterating BlogsPosts

It's just normal stuff. In MySQL you need to join if you want to select data from related table. You need to tell phalcon to join your related model.

Okay thanks for your answer :-)