こんにちは、最終的に請求書モデルの検証が 100% 機能するようになりましたが、間違ったテーブルのすべてを参照するようになったため、リレーションシップ モデル (機能していた) の検証が無効になりました。
リレーションシップ モデルは、ユーザーにリクエストを送信する前に、users テーブルにユーザーが存在することを確認することになっています。
請求書モデルは、ユーザーが関係テーブルに別のユーザーとの列を持っていることを確認することになっています。
どうすれば正しく動作するように変更できますか? 現時点では、コードはウェブサイトの私の関係全体を完全に台無しにしています.
-関係モデル
class Relationship extends AppModel
{
var $name = 'Relationship';
public $useTable = 'relationships_users';
public $primaryKey = 'id';
public $hasMany = 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 User extends AppModel{
public $name = 'User';
public $hasMany = array(
'Relationship' =>
array(
'className' => 'Relationship',
'joinTable' => 'relationships_users',
'foreignKey' => 'id',
'unique' => false,));
public $useTable = 'users';
public $primaryKey = 'id';
public $validate = array(
'username'=>array(
'The username must be between 5 and 15 characters.'=>array(
'rule'=>array('between', 5, 15),
'message'=>'The username must be between 5 and 15 characters.'
),
'That username has already been taken'=>array(
'rule'=>'isUnique',
'message'=>'That username has already been taken.'
)),
'email'=>array(
'Valid email'=>array(
'rule'=>array('email'),
'message'=>'Please enter a valid email address'
)
),
'password'=>array(
'Not Empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your password'
)
),
'Match passwords'=>array(
'rule'=>'matchPasswords',
'message'=>'Your passwords do not match'
)
,
'password_confirmation'=>array(
'Not Empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please confirm your password'
)));
public function matchPasswords($data){
if ($data['password']==$this->data['User']['password_confirmation']){
return true;
}
$this->invalidate('password_confirmation','Your passwords do not match');
return false;
}
public function beforeSave(){
if(isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
インボイスモデル-
class Invoice extends AppModel{
var $name='Invoice';
//public $useTable = 'invoices';
//public $primaryKey = 'id';
public $belongsTo = 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.'
),
),
);
var $validateTwo = array(
'datecreated'=>array(
'dateHasntExpired' => array(
'rule'=>array(
'dateHasntExpired',
),
'message'=> 'sorry but your relationship has expired.'
),
),
);
public function relationshipExists($check){
$relationshipExists=$this->Relationship->find('count', array(
'conditions' => array(
'Relationship.partyone <>' => current($check),
'Relationship.partytwo' => current($check),
// 'Relationship.active'==true,
// get the value from the passed var
)
));
if ($relationshipExists == true) {
return TRUE;
}
else
return FALSE;
}
public function dateHasntExpired($check){
$dateHasntExpired=$this->Relationship->find('count', array(
'conditions'=>array(
'DATE(Relationship.expirydate) > DATE(Invoice.datecreated)',
)));
if ($dateHasntExpired == true) {
return TRUE;
}
else
return FALSE;
}
}
現在の関係リクエストを表示しようとしたときに表示されるエラーは次のとおりです
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Invoice.invoice_id' in 'field list'
SQL Query: SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate`, `Invoice`.`invoice_id` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`invoice_id` IN (97, 98, 99, 101, 104, 105)
Notice: If you want to customize this error message, create
app\View\Errors\pdo_error.ctp
送信した関係リクエストを表示しようとすると、次のエラーが表示されます
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Invoice.invoice_id' in 'field list'
SQL Query: SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate`, `Invoice`.`invoice_id` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`invoice_id` IN (97, 98, 99, 101, 104, 105)
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp