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!