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

Retrieving joined DB table values gives error.

Hi all,

I got a question about JOINS in Phalcon, currently I got a (for example) "Cars" model:

<?php

    namespace Webapp\Frontend\Models;

    class Cars extends \Phalcon\Mvc\Model {

    public function initialize() {
        $this->hasMany('car_id', 'Parts', 'car_id');
    }

    // Search.
    public function search() {

        // Create query.
        $query = new \Phalcon\Mvc\Model\Query("
            SELECT DISTINCT
                Webapp\Frontend\Models\Cars.*
            FROM
                Webapp\Frontend\Models\Cars
                INNER JOIN Webapp\Frontend\Models\Parts
            LIMIT
                2
        ", $this->getDI());
        return $query->execute();

        etc...

And the "Parts" model:

<?php

    // Set namespace.
    namespace Webapp\Frontend\Models;

    // Use.
    use Phalcon\Mvc\Model\Resultset\Simple as Resultset;

    class Parts extends \Phalcon\Mvc\Model {

        public function initialize() {
            $this->belongsTo("car_id", "Cars", "car_id");
        }

    }

Now when I try to retrieve this in a Controller I can access the retrieved Car properties without any problem but when I try to acces the Parts I get the error "PhalconException: Model 'Parts' could not be loaded".

$cars = \Webapp\Frontend\Models\Cars::search();
foreach($cars as $car) {
    echo $car->name; // This works.
    if (isset($car->parts)) { // This works.
        foreach($car->parts as $part) { // Here the error is raised.

What can I be doing wrong here? When I add "Webapp\Frontend\Models\Parts.*" to the select query (or make it a LEFT JOIN) I get all kind of other errors.

Thank you for your time!



2.5k

In your initialization of the models make sure you include the namespaces that each ofh the models that it belongs to, I ran into a similar problem with my models.

edited Sep '14

Thank you for your reply, still can't get it to work though.

Changed: $this->hasMany('car_id', 'Parts', 'car_id'); To: $this->hasMany('car_id', 'Webapp\Frontend\Models\Parts', 'car_id'); (and also changed the parts initialize)

Allthough I don't get an error anymore it doesn't "find" the $car->parts item either. So it just doesn't iterate. Could be because I've not selected the parts in the search method of the Cars model, but when I add that like this:

SELECT DISTINCT
    Webapp\Frontend\Models\Cars.*,
    Webapp\Frontend\Models\Parts.*
FROM
    Webapp\Frontend\Models\Cars
INNER JOIN Webapp\Frontend\Models\Parts
LIMIT
    2

I get another error: Notice: Undefined property: Phalcon\Mvc\Model\Row::$name (@ printing the cars name which doesn't seem available anymore after I added Webapp\Frontend\Models\Parts.* to the query.



2.5k

Your probably getting that error because you have a name column in both models?



2.5k

By the way i was able to pull parts using the following method on the models:

       $cars = Cars::find();

       foreach($cars as $car)
       {
           $parts = $car->parts;
       }

       $this->view->result = $cars;
       $this->view->parts = $parts;


2.7k
Accepted
answer
edited Nov '14

I got it to work by adding an "alias" to the relationship and using that alias to retrieve from related models. Not really sure why it worked with the alias and not without it.

Thank you for your time.