新しい請求書をデータベースに挿入しようとすると、cakephp が where 句の relationship.partytwo 列が見つからないというエラーをスローします。私が達成しようとしているのは、請求書を送信するユーザーが、請求書が送信される前に請求書を受信した人との関係が関係_ユーザーデータベースにあることを確認することです.悲しいことに、私が得ているのはデータベースエラーだけです.
ここに私のテーブルの構造があります
invoices - id, to, biller, subject, description, datecreated, duedate
relationships_users - id, partyone, partytwo, expirydate,active
ここに関係モデルがあります
<?php
class Relationship extends AppModel
{
var $name = 'Relationship';
public $useTable = 'relationships_users';
public $primaryKey = 'id';
public $belongsTo = array(
'Invoice' =>
array(
'className' => 'Invoice',
'joinTable' => 'invoice',
'foreignKey' => 'invoice_id'));
public $belongsTo = array(
'User' =>array(
'className' => 'User',
'foreignKey' =>'partyone','partytwo',
'associationForeignKey' => 'username',
));
var $validate = array(
'date' => array(
'rule' => array(
'datevalidation',
'systemDate'
),
'message' => 'Current Date and System Date is mismatched'
),
'partytwo' => array(
'userExists' => array(
'rule' => array(
'userExists',
),
'message' => 'That username doesnt exist.'
),
),
);
function datevalidation($field = array(), $compare_field = null)
{
if ($field['date'] > $compare_field)
return TRUE;
else
return FALSE;
}
function userExists($check)
{
$userExists = $this->User->find('count', array('conditions' => array('User.username'=>$check)));
if ($userExists == 1) {
return TRUE;
}
else
return FALSE;
}
}
ここに私の請求書モデルがあります
<?php
App::uses('AuthComponent', 'Controller/Component');
class Invoice extends AppModel{
var $name='Invoice';
public $useTable = 'invoices';
public $primaryKey = 'id';
public $hasMany = array(
'Relationship' =>array(
'className' => 'Relationship',
'foreignKey' =>'relationship_id',
));
var $validate = array(
'to' => array(
'relationshipExists' => array(
'rule' => array(
'relationshipExists',
),
'message' => 'sorry you dont have a relationship with that user.'
),
),
);
public function relationshipExists($check){
if($this->hasAny(array('Relationship.partytwo'=>current($check))))
{
return TRUE;
}
else
{
return FALSE;
}
}
}
これが私のaddinvoice関数です
public function addinvoice(){
$this->set('title_for_layout', 'Create Invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
if($this->request->is('post')){
pr($this->Invoice->set($this->request->data));
if($this->Invoice->validates(array('fieldList'=>array('to','Invoice.relationshipExists')))){
// $this->Relationship->create();
$this->Invoice->save($this->request->data);
$this->Session->setFlash('The invoice has been saved');
}}else {
$this->Session->setFlash('The invoice could not be saved. Please, try again.');
}
}