CakePHP 1.3 - Unknown column in where clause

前端 未结 5 752
情话喂你
情话喂你 2021-01-18 02:02

I\'m working on an already existing cakephp 1.3 project and I needed to add a new table to the database. I have this in my controller:

    $conditions = arra         


        
相关标签:
5条回答
  • 2021-01-18 02:15

    You should be very careful with the relationship you're after. From a quick glance at some of these answers, they seem to suggest you simply add a join to the Email model into your Person model and rely on the conditions of your find to ensure your query doesn't ransack your server's memory.

    I'm going to assume that first of all, you want this Email relationship to be implicit in all your queries on Person, otherwise you could simply specify the join on each query you wanted it for. In this case, you definitely want to link it using model relationships.

    Your code shows that Shooting and ShootingPlacement (presume this is a model to model mapping relationship) both belong to two models. Incidentally, Shooting belongsTo Emission - which we haven't seen here yet. I assume this isn't applicable to the current scenario.

    Now, let's assume off the bad that because your Email table has foreign keys, it will be a hasOne relationship, rather than a hasMany - so that's what you need to link it by. I'm going to link it to the ShootingPlacement model because this is the model you are querying, so it should be the central point at which models are joined around it. Structure wise, because everything seems to originate from your Person model, I would have to suggest you query that model instead. But the way it's set up so far will allow you to query from nearly anywhere and still retrieve mostly the same results bar a few model names and table aliases.

    Purely because your foreign key between Email and ShootingPlacement has a different name, and CakePHP 1.3 doesn't handle this very well, I'm also going to suggest you don't use a foreign key, instead putting it into the relationship as conditions.

    class ShootingPlacement extends AppModel
    {
        var $name = 'ShootingPlacement';
        var $actsAs = array('Containable');
    
        var $hasOne = array(
            'Email' => array(
                'className' => 'Email',
                'foreignKey' => false,
                'conditions' => array(
                    'Email.shooting_placement_id = ShootingPlacement.id',
                    'Email.person_id = ShootingPlacement.person_id'
                )
            )
        );
    
        var $belongsTo = array (
            'Person' => array (
                'className' => 'Person',
                'foreignKey' => 'person_id',
                'order' => 'lastname ASC'
            ),
            'Shooting' => array (
                'className' => 'Shooting',
                'foreignKey' => 'shooting_id'
            )
        );
    }
    

    I've also added the containable behaviour in there. This allows you to control from each query which associated models you'd like to return with your primary model results. It will default to all, but can be handy when you only want something specific and/or for memory reasons (these kinds of queries can destroy your server memory pretty quickly if you don't limit them or specify only the field names you want to return).

    Now when you create your Email model, I wouldn't suggest complicating this mess of entangled models any further by linking it back to ShootingPlacement again. As you've said, it also has a foreign key to the Person model. So you might want to do exactly the same thing as above for your Person model (changing the conditions to reflect the Person foreign key of course). This way your model is a little more flexible; it will still join to ShootingPlacement and Person, and will also allow you to query it seperately if required without the other associated models.

    Documentation

    • CakePHP 1.3 Model Associations
    • CakePHP 1.3 Containable Behaviour

    See also

    • This article on Stack
    0 讨论(0)
  • 2021-01-18 02:19

    You need to link your model ShootingPlacement with "Email" with which you call it.

    class ShootingPlacement extends AppModel
    
    var $name = 'Shooting';
    
    var $hasMany= array
    (
        'Email' => array
        (
            'className' => 'Email',
            'foreignKey' => 'yourfk'
        ),
       );
    }
    

    And uses it s very powerful ContainableBehavior !

    exemple :

    $contain=array('Email'=>array('fields'=>array('id','...')));
    $conditions=array('ShootingPlacement.id'=>$yourId);
    $this->ShootingPlacement->attachBehaviros('Containable');
    $this->ShootingPlacement->find('all',$conditions);// your will retrieve yoru SHootingItem + Emails linked 
    
    0 讨论(0)
  • 2021-01-18 02:30

    In your model add containable behavior

    class Email extends AppModel {
        var $name = 'Email';
    
        var $actsAs = array('Containable');
    
        var $belongsTo = array
        (
            'Person' => array
            (
                'className' => 'Person',
                'foreignKey' => 'person_id'
            ),
            'ShootingPlacement' => array
            (
                'className' => 'ShootingPlacement',
                'foreignKey' => 'shooting_placement_id'
            )
        );
    }
    

    Just write the below code in your controller.

    $this->ShootingPlacement->recursive = 2;
    $this->ShootingPlacement->contain = array(
        'Shooting',
        'Person' => array(
            'Email'
        )
    );
    $conditions = array(
      'ShootingPlacement.person_id' => $id,
      'Email.shooting_placement_id' => 'ShootingPlacement.id'
    );
    $shootingPlacements = $this->ShootingPlacement->find('all', compact('conditions'));
    

    Hope this helps you.

    0 讨论(0)
  • 2021-01-18 02:30

    This would provide the required join:

    $conditions = array('ShootingPlacement.person_id' => $id, 'Email.person_id' => $id, 'Email.shooting_placement_id' => 'ShootingPlacement.id');
    
    $joins = array(
       array(
        'table' => 'emails', 
        'alias' => 'Email', 
        'type' => 'LEFT', 
        'conditions' => array('Email.shooting_placement_id = ShootingPlacement.id')
      )
    ); 
    
    
    
    $shootingPlacements = $this->ShootingPlacement->find('all', 
       array(
          'conditions' => $conditions,
          'joins' => $joins
       )
    );
    
    0 讨论(0)
  • 2021-01-18 02:34

    Add a $hasOne relation to Person model with Email like below

    var $hasOne = array(
        'Email' => array(
                 'className' => 'Email',
                 'foreignKey' => 'person_id' // Column defined for person ids in Email table
                 )
    );
    

    Then add

    $this->ShootingPlacement->recursive = 2;
    

    OR you can simply use joins in cakephp to join email model. Refer cakephp joining tables

    0 讨论(0)
提交回复
热议问题