PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to get datas from related tables in Phalcon?

 October 21, 2022     foreign-keys, has-many, phalcon, php, relationship     No comments   

Issue

I have exactly the same structure like in the phalcon models documentation: http://docs.phalconphp.com/en/latest/_images/eer-1.png

In the models I implemented the following hasmany and belongsto lines:

Robots model:

class Robots extends \Phalcon\Mvc\Model
{
    public $id;
    public $name;
    public function initialize(){
        $this->hasMany("id", "RobotsParts", "robots_id");
    }
}

Parts model:

class Parts extends \Phalcon\Mvc\Model
{
    public $id;
    public $name;
    public function initialize(){
        $this->hasMany("id", "RobotsParts", "parts_id");
    }
}

RobotParts model:

class RobotsParts extends \Phalcon\Mvc\Model
{
    public $id;
    public $robots_id;
    public $parts_id;
    public function initialize(){
        $this->belongsTo("robots_id", "Robots", "id");
        $this->belongsTo("parts_id", "Parts", "id");
    }
} 

At this point I was hoping to get all the data by calling RobotParts::find(), but I can see only the id's. For debuging I dumped, but find only the ids:(

$rp = RobotParts::find()->toArray();
var_dump($rp);

I would like to get something like this as result:

array (size=1)
  0 => 
    array (size=7)
      'id' => int '1' (length=1)
      'robots_id' => int '4' (length=1)
          'name'  => string 'r2d2' (length=4)
          'type'  => string 'droid' (length=5)
          'year'  => int '2184' (length=4)
      'parts_id' => int '4' (length=1)
          'name'  => string 'wheel' (length=5)

Solution

var_dump() does not contains the related tables, needed to reference to it from view like:

robots.RobotParts.name


Answered By - b4l4zs
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing