public abstract static string[] primaryKey ( ) | ||
傳回 | string[] |
此 AR 類別的主鍵名稱。 |
---|
public static function primaryKey();
繼承自 | yii\base\StaticInstanceInterface |
---|---|
實作於 | yii\db\ActiveRecord, yii\db\BaseActiveRecord |
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveRecordInterface.php |
ActiveRecordInterface 介面。
從資料庫刪除記錄。
public abstract integer|boolean delete ( ) | ||
傳回 | integer|boolean |
刪除的列數,或如果刪除因某些原因不成功,則返回 |
---|
public function delete();
使用提供的條件刪除記錄。
警告:如果您未指定任何條件,此方法將刪除表格中的所有列。
例如,刪除所有狀態為 3 的客戶
Customer::deleteAll([status = 3]);
public abstract static integer deleteAll ( $condition = null ) | ||
$condition | array|null |
符合應刪除記錄的條件。 請參閱 yii\db\QueryInterface::where() 以了解如何指定此參數。 空條件將符合所有記錄。 |
傳回 | integer |
刪除的列數 |
---|
public static function deleteAll($condition = null);
傳回一個值,指示給定的 active record 是否與目前的 active record 相同。
兩個 new 記錄被認為不相等。
public abstract boolean equals ( $record ) | ||
$record | static |
要比較的記錄 |
傳回 | boolean |
兩個 active record 是否指向同一個資料庫表格中的同一列。 |
---|
public function equals($record);
建立一個 yii\db\ActiveQueryInterface 實例,用於查詢目的。
傳回的 yii\db\ActiveQueryInterface 實例可以通過在呼叫 one()
或 all()
以傳回填充的 ActiveRecord 實例之前,呼叫 yii\db\ActiveQueryInterface 中定義的方法來進一步自訂。 例如,
// find the customer whose ID is 1
$customer = Customer::find()->where(['id' => 1])->one();
// find all active customers and order them by their age:
$customers = Customer::find()
->where(['status' => 1])
->orderBy('age')
->all();
此方法也由 yii\db\BaseActiveRecord::hasOne() 和 yii\db\BaseActiveRecord::hasMany() 呼叫,以建立關聯式查詢。
您可以覆寫此方法以傳回自訂查詢。 例如,
class Customer extends ActiveRecord
{
public static function find()
{
// use CustomerQuery instead of the default ActiveQuery
return new CustomerQuery(get_called_class());
}
}
以下程式碼示範如何為所有查詢套用預設條件
class Customer extends ActiveRecord
{
public static function find()
{
return parent::find()->where(['deleted' => false]);
}
}
// Use andWhere()/orWhere() to apply the default condition
// SELECT FROM customer WHERE `deleted`=:deleted AND age>30
$customers = Customer::find()->andWhere('age>30')->all();
// Use where() to ignore the default condition
// SELECT FROM customer WHERE age>30
$customers = Customer::find()->where('age>30')->all();
public abstract static yii\db\ActiveQueryInterface find ( ) | ||
傳回 | yii\db\ActiveQueryInterface |
新建立的 yii\db\ActiveQueryInterface 實例。 |
---|
public static function find();
傳回符合指定主鍵值或一組欄位值的 active record 模型清單。
此方法接受
WHERE
條件。['id' => 1, 2]
被視為非關聯陣列。 欄位名稱僅限於 SQL DBMS 的目前記錄表格欄位,否則會過濾以限制為簡單的篩選條件。此方法將自動呼叫 all()
方法,並傳回 ActiveRecord 實例的陣列。
注意:由於這僅是一個簡寫方法,因此使用更複雜的條件(例如 ['!=', 'id', 1])將不起作用。 如果您需要指定更複雜的條件,請改用 find() 與 where() 結合使用。
請參閱以下程式碼以取得使用範例
// find the customers whose primary key value is 10
$customers = Customer::findAll(10);
// the above code is equivalent to:
$customers = Customer::find()->where(['id' => 10])->all();
// find the customers whose primary key value is 10, 11 or 12.
$customers = Customer::findAll([10, 11, 12]);
// the above code is equivalent to:
$customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
// find customers whose age is 30 and whose status is 1
$customers = Customer::findAll(['age' => 30, 'status' => 1]);
// the above code is equivalent to:
$customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
如果您需要將使用者輸入傳遞給此方法,請確保輸入值是純量,或者在陣列條件的情況下,請確保陣列結構無法從外部變更
// yii\web\Controller ensures that $id is scalar
public function actionView($id)
{
$model = Post::findOne($id);
// ...
}
// explicitly specifying the colum to search, passing a scalar or array here will always result in finding a single record
$model = Post::findOne(['id' => Yii::$app->request->get('id')]);
// do NOT use the following code! it is possible to inject an array condition to filter by arbitrary column values!
$model = Post::findOne(Yii::$app->request->get('id'));
public abstract static array findAll ( $condition ) | ||
$condition | mixed |
主鍵值或一組欄位值 |
傳回 | array |
ActiveRecord 實例的陣列,或如果沒有任何符合項,則為空陣列。 |
---|
public static function findAll($condition);
透過主鍵或欄位值陣列,傳回單一 active record 模型實例。
此方法接受
null
)。null
)。null
)。 請注意,['id' => 1, 2]
被視為非關聯陣列。 欄位名稱僅限於 SQL DBMS 的目前記錄表格欄位,否則會過濾以限制為簡單的篩選條件。此方法將自動呼叫 one()
方法,並傳回 ActiveRecord 實例。
注意:由於這僅是一個簡寫方法,因此使用更複雜的條件(例如 ['!=', 'id', 1])將不起作用。 如果您需要指定更複雜的條件,請改用 find() 與 where() 結合使用。
請參閱以下程式碼以取得使用範例
// find a single customer whose primary key value is 10
$customer = Customer::findOne(10);
// the above code is equivalent to:
$customer = Customer::find()->where(['id' => 10])->one();
// find the customers whose primary key value is 10, 11 or 12.
$customers = Customer::findOne([10, 11, 12]);
// the above code is equivalent to:
$customers = Customer::find()->where(['id' => [10, 11, 12]])->one();
// find the first customer whose age is 30 and whose status is 1
$customer = Customer::findOne(['age' => 30, 'status' => 1]);
// the above code is equivalent to:
$customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
如果您需要將使用者輸入傳遞給此方法,請確保輸入值是純量,或者在陣列條件的情況下,請確保陣列結構無法從外部變更
// yii\web\Controller ensures that $id is scalar
public function actionView($id)
{
$model = Post::findOne($id);
// ...
}
// explicitly specifying the colum to search, passing a scalar or array here will always result in finding a single record
$model = Post::findOne(['id' => Yii::$app->request->get('id')]);
// do NOT use the following code! it is possible to inject an array condition to filter by arbitrary column values!
$model = Post::findOne(Yii::$app->request->get('id'));
public abstract static static|null findOne ( $condition ) | ||
$condition | mixed |
主鍵值或一組欄位值 |
傳回 | static|null |
符合條件的 ActiveRecord 實例,或如果沒有任何符合項,則為 |
---|
public static function findOne($condition);
public abstract mixed getAttribute ( $name ) | ||
$name | string |
屬性名稱 |
傳回 | mixed |
屬性值。 如果未設定或不存在屬性,則為 |
---|
public function getAttribute($name);
傳回此 AR 類別使用的連線。
public abstract static mixed getDb ( ) | ||
傳回 | mixed |
此 AR 類別使用的資料庫連線。 |
---|
public static function getDb();
傳回一個值,指示目前的記錄是否為新記錄(尚未儲存到資料庫)。
public abstract boolean getIsNewRecord ( ) | ||
傳回 | boolean |
記錄是否為新的,且應在呼叫 save() 時插入。 |
---|
public function getIsNewRecord();
傳回舊的主鍵值。
這指的是在執行 find 方法(例如 find()、findOne())後填入記錄的主鍵值。 即使主鍵屬性是以不同的值手動指派,此值仍保持不變。
public abstract mixed getOldPrimaryKey ( $asArray = false ) | ||
$asArray | boolean |
是否將主鍵值作為陣列傳回。 如果為 true,則傳回值將是陣列,其中欄位名稱作為鍵,欄位值作為值。 如果此值為 |
傳回 | mixed |
舊的主鍵值。 如果主鍵是複合的或 |
---|
public function getOldPrimaryKey($asArray = false);
傳回主鍵值。
public abstract mixed getPrimaryKey ( $asArray = false ) | ||
$asArray | boolean |
是否將主鍵值作為陣列傳回。 如果為 true,則傳回值將是陣列,其中屬性名稱作為鍵,屬性值作為值。 請注意,對於複合主鍵,無論此參數值為何,都將始終傳回陣列。 |
傳回 | mixed |
主鍵值。 如果主鍵是複合的或 |
---|
public function getPrimaryKey($asArray = false);
傳回具有指定名稱的關聯物件。
關聯是由 getter 方法定義的,該方法傳回實作 yii\db\ActiveQueryInterface 的物件(通常這會是關聯式 yii\db\ActiveQuery 物件)。 它可以在 ActiveRecord 類別本身或其行為之一中宣告。
public abstract yii\db\ActiveQueryInterface getRelation ( $name, $throwException = true ) | ||
$name | string |
關聯名稱,例如透過 |
$throwException | boolean |
如果關聯不存在是否擲回例外。 |
傳回 | yii\db\ActiveQueryInterface |
關聯式查詢物件 |
---|
public function getRelation($name, $throwException = true);
傳回一個值,指示記錄是否具有指定名稱的屬性。
public abstract boolean hasAttribute ( $name ) | ||
$name | string |
屬性的名稱 |
傳回 | boolean |
記錄是否具有指定名稱的屬性。 |
---|
public function hasAttribute($name);
使用此記錄的屬性值,將記錄插入資料庫。
使用範例
$customer = new Customer;
$customer->name = $name;
$customer->email = $email;
$customer->insert();
public abstract boolean insert ( $runValidation = true, $attributes = null ) | ||
$runValidation | boolean |
是否在儲存記錄之前執行驗證(呼叫 validate())。 預設為 |
$attributes | array|null |
需要儲存的屬性清單。 預設為 |
傳回 | boolean |
屬性是否有效且記錄是否插入成功。 |
---|
public function insert($runValidation = true, $attributes = null);
定義於: yii\base\StaticInstanceInterface::instance()
傳回靜態類別實例,可用於取得元資訊。
public abstract static static instance ( $refresh = false ) | ||
$refresh | boolean |
即使已快取靜態實例,是否重新建立靜態實例。 |
傳回 | static |
類別實例。 |
---|
public static function instance($refresh = false);
傳回一個值,指示給定的一組屬性是否代表此模型的主鍵。
public abstract static boolean isPrimaryKey ( $keys ) | ||
$keys | array |
要檢查的屬性集 |
傳回 | boolean |
給定的屬性集是否代表此模型的主鍵 |
---|
public static function isPrimaryKey($keys);
建立兩個記錄之間的關聯。
透過將一個記錄中的外鍵值設定為另一個記錄中對應的主鍵值來建立關聯。 具有外鍵的記錄將儲存到資料庫中,而無需執行驗證。
如果關聯涉及連接表格,則將在連接表格中插入一個新列,其中包含來自兩個記錄的主鍵值。
此方法要求主鍵值不得為 null
。
public abstract void link ( $name, $model, $extraColumns = [] ) | ||
$name | string |
關聯的區分大小寫名稱,例如透過 |
$model | static |
要與目前記錄連結的記錄。 |
$extraColumns | array |
要儲存到連接表格中的其他欄位值。 此參數僅對於涉及連接表格的關聯(即使用 yii\db\ActiveQueryInterface::via() 設定的關聯)才有意義。 |
public function link($name, $model, $extraColumns = []);
使用相關記錄填充指定的關聯。
請注意,此方法不會檢查關聯是否存在。
public abstract void populateRelation ( $name, $records ) | ||
$name | string |
關聯名稱,例如透過 |
$records | yii\db\ActiveRecordInterface|array|null |
要填入關聯的相關記錄。 |
public function populateRelation($name, $records);
public abstract static string[] primaryKey ( ) | ||
傳回 | string[] |
此 AR 類別的主鍵名稱。 |
---|
public static function primaryKey();
儲存目前的記錄。
當 isNewRecord 為 true 時,此方法將呼叫 insert();當 isNewRecord 為 false 時,則呼叫 update()。
例如,儲存客戶記錄
$customer = new Customer; // or $customer = Customer::findOne($id);
$customer->name = $name;
$customer->email = $email;
$customer->save();
public abstract boolean save ( $runValidation = true, $attributeNames = null ) | ||
$runValidation | boolean |
是否在儲存記錄之前執行驗證(呼叫 validate())。 預設為 |
$attributeNames | array|null |
需要儲存的屬性名稱清單。 預設為 |
傳回 | boolean |
儲存是否成功(即,未發生驗證錯誤)。 |
---|
public function save($runValidation = true, $attributeNames = null);
設定指定的屬性值。
另請參閱 hasAttribute()。
public abstract void setAttribute ( $name, $value ) | ||
$name | string |
屬性名稱。 |
$value | mixed |
屬性值。 |
public function setAttribute($name, $value);
解除兩個記錄之間的關聯。
如果 $delete
為 true,則會刪除具有關聯外鍵的記錄。 否則,外鍵將設定為 null
,且記錄將在未驗證的情況下儲存。
public abstract void unlink ( $name, $model, $delete = false ) | ||
$name | string |
關聯的區分大小寫名稱,例如透過 |
$model | static |
要從目前記錄取消連結的模型。 |
$delete | boolean |
是否刪除包含外鍵的模型。 如果為 false,則模型的外鍵將設定為 |
public function unlink($name, $model, $delete = false);
將此 active record 的變更儲存到資料庫。
使用範例
$customer = Customer::findOne($id);
$customer->name = $name;
$customer->email = $email;
$customer->update();
public abstract integer|boolean update ( $runValidation = true, $attributeNames = null ) | ||
$runValidation | boolean |
是否在儲存記錄之前執行驗證(呼叫 validate())。 預設為 |
$attributeNames | array|null |
需要儲存的屬性清單。 預設為 |
傳回 | integer|boolean |
受影響的列數,或如果驗證失敗或更新程序因其他原因停止,則返回 |
---|
public function update($runValidation = true, $attributeNames = null);
使用提供的屬性值和條件更新記錄。
例如,將所有狀態為 2 的客戶的狀態變更為 1
Customer::updateAll(['status' => 1], ['status' => '2']);
public abstract static integer updateAll ( $attributes, $condition = null ) | ||
$attributes | array |
要為記錄儲存的屬性值 (名稱-值 組)。與 update() 不同,這些屬性值不會被驗證。 |
$condition | mixed |
符合要更新記錄的條件。請參考 yii\db\QueryInterface::where() 以了解如何指定此參數。空的條件將會匹配所有記錄。 |
傳回 | integer |
更新的列數 |
---|
public static function updateAll($attributes, $condition = null);
若要留言,請註冊或登入。