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

Can't obtain model's source

I have a model with a hasManyToMany() relationship. I'm trying to retrieve some of the related models by adding a condition, but I'm getting an exception (formatted for readability):

Phalcon\Mvc\Model\Exception: Can't obtain model's source from models list: 'Model\MailingList', when preparing: 
SELECT 
    [\Model\MailingList].* 
FROM 
    [\Model\MailingList] 
INNER JOIN 
    [\Model\EmailMailingList] ON 
    [\Model\EmailMailingList].[mailing_list_id] = [\Model\MailingList].[id] 
WHERE 
    (queued = 0) AND
    ([\Model\EmailMailingList].[email_id] = :APR0:)

Here is my relationship setup:

$this->hasManyToMany(
            'id'
            ,'\Model\EmailMailingList'
            ,'email_id','mailing_list_id'
            ,'\Model\MailingList'
            ,'id'
            ,['alias'=>'MailingLists']
        );

And my method call to the relationship:

$this->getMailingLists('queued = 0');

If I just call $this->getMailingLists() the query works fine. Adding the condition causes the error listed above. Just to be sure, I added a getSource() method to the MailingList class.

Is this a bug? I'm running 3.4.2.

Update When I duplicate the query using the query builder, it works fine:

SELECT
    \Model\MailingList.*
FROM
    \Model\MailingList
INNER JOIN
    \Model\EmailMailingList ON
    \Model\EmailMailingList.mailing_list_id = \Model\MailingList.id
WHERE
    queued = 0 AND
    \Model\EmailMailingList.email_id = :email_id:


1.7k

Try $this->getMailingLists(['queued = 0']); instead of $this->getMailingLists('queued = 0');