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

Tuesday, March 15, 2022

[FIXED] HasOne and HasMany for the same Table cakephp

 March 15, 2022     cakephp, cakephp-3.0, orm, php, sql     No comments   

Issue

i'd like to set hasOne and hasMany to same model,in a part of my code i need only 1 result, but in other part i need all result (Objects from type Client that will return for a table in my site):

    $this->hasOne('Vendas')
    ->setForeignKey('id_cliente')
    ->setBindingKey('id')
    ;

    $this->hasMany('Vendas')
    ->setForeignKey('id_cliente')
    ->setBidingKey('id');

This is possible, or i made a mistake?


Solution

Read the manual https://book.cakephp.org/3.0/en/orm/associations.html. Read the whole page carefully.

  • className: the class name of the table being associated to the current model. If you’re defining a ‘User hasOne Address’ relationship, the className key should equal ‘Addresses’.
  • conditions: an array of find() compatible conditions such as ['Addresses.primary' => true]

Define the class name and conditions you need for your assocs.

    $this->hasOne('Foo', [
        'className' => 'Foo',
        'conditions' => [/* whatever you need*/]
    ]);

    $this->hasMany('Bar', [
        'className' => 'Foo',
        'conditions' => [/* whatever you need*/]
    ]);


Answered By - floriank
  • 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