1

I've been working with Phalcon and slowly getting to grips with it. However, I've stumbled across an issues and it's got me stumped. I'm hoping that someone else can provide some assistance.

I have two tables in the DB that are related as a one-to-many. clients->sites. These are the two definitions of the models in Phalcon:

#File: CrmClients.php
namespace CRM\Models;
use Phalcon\Mvc\Model\Resultset\Simple as Resultset;

class CrmClients extends \Phalcon\Mvc\Model
{
    public id;

    public function initialize()
    {
        $this->hasMany("id", "CRM\Models\CrmSites", "client_id", array("alias" => "Sites"));
    }
}

#File: CrmSites.php
namespace CRM\Models;

class CrmSites extends \Phalcon\Mvc\Model
{
    public id;
    public client_id;

    public function initialize()
    {
        $this->belongsTo("client_id", "CRM\Models\CrmClients", "id", array("foreignKey" => true, "alias" => "Clients"));
    }
}

Then in the controller I have:

$profile = Clients::findFirstById($id);
$sites = $profile->Sites;

When I run this I end up with the following error:

Notice: Access to undefined property CRM\Models\CrmClients::Sites in \html\apps\crm\controllers\ClientsController.php on line 51

I'm at a loss as to what I'm doing wrong here, and any help would be greatly appreciated.

If you have any questions, or need any clarification, please just ask.

Thanks for your help in advance.

rabrowne
  • 61
  • 6

2 Answers2

2

After much reviewing and some help from the Phalcon forums also. It transpired that there's nothing wrong with the code.

The problem came down to a "user error" in that I had two copies of the CrmClients.php file in two separate modules. I was editing the CrmClients in the wrong folder, and as such the CrmClients.php file with the CrmSites.php didn't have an initialize function at all - probably why it wasn't finding the relationship...

As such, I added the initialize function and it's working perfectly.

@digitronic: really appreciate the help on this one.

Thanks again.

rabrowne
  • 61
  • 6
0

You forgot third param in your hasMany() method in CrmClients model.

you code looks like:

$this->hasMany("id", "CRM\Models\CrmSites", array("alias" => "Sites"));

and it should be like:

$this->hasMany("id", "CRM\Models\CrmSites", 'client_id', array("alias" => "Sites"));

seems to me that could be related to error that you are getting ...

  • thanks for the response. My apologies; in transcribing it from the source to the site that got parameter got deleted. That is in my code and it still doesn't work. I have updated my original post to suit. – rabrowne Aug 05 '14 at 15:24
  • could you try with using $profile->getRelated('Sites') instead of $profile->Sites , might give you more descriptive error – digitronac Aug 06 '14 at 11:28
  • this changes the message to the following: `There is no defined relations for the model "CRM\Models\CrmClients" using alias "Sites"` – rabrowne Aug 06 '14 at 15:26
  • I can't be sure that the "initialize()" method is being called. Is there anyway I can check if it is? – rabrowne Aug 06 '14 at 19:50
  • strange ... i use namespaces and define relationships same like you, and never had problems. only difference is that i use lowercased alias. double check if order of params is right (id/client_id) , besides that im clueless – digitronac Aug 06 '14 at 20:24
  • I've tried changing to lowercase aliases but not change. Thanks for your help though. – rabrowne Aug 07 '14 at 16:19