Frost BYTE
Frost BYTE

Reputation: 11

Relationship in cakephp3

I am trying to make the relationship between the tables I read The documentations in cakephp3, and tons of post in stackoverflow, and still can’t get any result. I have two tables.

I want to display every element of portofolio with category like this:

Category Cars

* element 1: Bmw
* element 2: Audi
* element 3: Ford

How can i make this relation and display?

In categories model I write :

 $this->belongsTo('Portfolios', [
              'foreignKey' => 'id'
          ]);

And this is the query:

$portfolios_category = $this->Categories->find()
            ->contain(['Portfolios'])
            ->where(['Categories.published' => 1])
            ->order(['Categories.created' => 'desc'])
            ->all();

Upvotes: 1

Views: 44

Answers (2)

Shifat
Shifat

Reputation: 762

Just one correction :

 $this->belongsTo('Portfolios', [
              'foreignKey' => 'category_id'
 ]);

After that loop through $portfolios_category , hope you will get the result.

Upvotes: 1

RiTeSh
RiTeSh

Reputation: 521

Start using the command prompt. Go to your project_folder/bin/ and open command prompt.

Then type

cake bake model

then you will get the list of Table with model name, then type

cake bake model <ModelName>

ex:

cake bake model Category
cake bake model Portfolio 

Upvotes: 0

Related Questions