문제

I have three models in question: Customer, Company and User. Customer and User both belong to Company and Company has many Customers as following:

Customer:

    var $belongsTo = array(
            'Company' => array(
            'className' => 'Company',
            'foreignKey' => 'company_id',
            'dependent' => false,
        ),
    );

Company:

        var $hasMany = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'company_id',
        'dependent' => false
    ),
    'Customer'=>array(
        'className' => 'Customer',
        'foreignKey' => 'company_id',
        'dependent' => false
    )
);

User:

        var $belongsTo = array(
        'Company' => array(
        'className' => 'Company',
        'foreignKey' => 'company_id',
        'dependent' => false,
    ),
);

I have a problem when creating/editing Customer objects. Here is how to create form looks like:

    echo $this->Form->input('Customer.customer_nr');
    echo $this->Form->input('Customer.name');
    echo $this->Form->input('Customer.phone');
    echo $this->Form->input('Customer.email');
    echo $this->Form->input('Customer.address');
    echo $this->Form->input('Customer.post_nr');
    echo $this->Form->input('Customer.city');
    echo $this->Form->input('Customer.company_id', array('value' => $current_user['company_id'], 'type'=>'hidden'));

What I do in the end of the form is I take company_id from a currently logged in user and insert it as a Customer.company_id. It used to work without any problems before the new relations have been introduced. But now as I try to create/edit Customer, I receive the following SQL error:

Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous

Any help is much much appreciated.

Here is the controller add function:

function add() {



if (!empty($this->data) ) {
    $this->Customer->create();
    if ($this->Customer->save($this->data)) {
        $this->Session->setFlash(__('Customer was saved'), 'positive_notification');
        $this->redirect(array('controller'=>'events', 'action' => 'dashboard'));
    } else {
        $this->Session->setFlash(__('Customer has been saved. Please, try again'), 'negative_notification');
    }
}

}

The error is definately not being cause by redirect as it was fully tested.

도움이 되었습니까?

해결책

the problem is somewhere else.

It's in fact related to a find() call.

Try to locate the exact code that trigger the error and post it in your question.

Probably you set some conditions like

'conditions' => array(
    'name' => 'john' 
)

but you better do something like

'conditions' => array(
    'User.name' => 'john' 
)

after you created the relationship between User and Company (it's just an example, maybe the two tabler involved are others) Cake started to join the two tables. So when you search for a particular name mysql doesn't know if you want user name or company name because you have name column in both tables.

If you look at the generated query (the one that gives you that error) you'll see the two tables joined. If you don't want that join you have to specify recursive => -1

'conditions' => array(
    'name' => 'john' 
),
'recursive' => -1
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top