0 追蹤者

介面 yii\db\ActiveRecordInterface

繼承自yii\base\StaticInstanceInterface
實作於yii\db\ActiveRecord, yii\db\BaseActiveRecord
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveRecordInterface.php

ActiveRecordInterface 介面。

公開方法

隱藏繼承的方法

方法 描述 定義於
attributes() 傳回記錄中所有屬性名稱的清單。 yii\db\ActiveRecordInterface
delete() 從資料庫刪除記錄。 yii\db\ActiveRecordInterface
deleteAll() 使用提供的條件刪除記錄。 yii\db\ActiveRecordInterface
equals() 傳回一個值,指示給定的 active record 是否與目前的 active record 相同。 yii\db\ActiveRecordInterface
find() 建立一個 yii\db\ActiveQueryInterface 實例,用於查詢目的。 yii\db\ActiveRecordInterface
findAll() 傳回符合指定主鍵值或一組欄位值的 active record 模型清單。 yii\db\ActiveRecordInterface
findOne() 透過主鍵或欄位值陣列,傳回單一 active record 模型實例。 yii\db\ActiveRecordInterface
getAttribute() 傳回指定的屬性值。 yii\db\ActiveRecordInterface
getDb() 傳回此 AR 類別使用的連線。 yii\db\ActiveRecordInterface
getIsNewRecord() 傳回一個值,指示目前的記錄是否為新記錄(尚未儲存到資料庫)。 yii\db\ActiveRecordInterface
getOldPrimaryKey() 傳回舊的主鍵值。 yii\db\ActiveRecordInterface
getPrimaryKey() 傳回主鍵值。 yii\db\ActiveRecordInterface
getRelation() 傳回具有指定名稱的關聯物件。 yii\db\ActiveRecordInterface
hasAttribute() 傳回一個值,指示記錄是否具有指定名稱的屬性。 yii\db\ActiveRecordInterface
insert() 使用此記錄的屬性值,將記錄插入資料庫。 yii\db\ActiveRecordInterface
instance() 傳回靜態類別實例,可用於取得元資訊。 yii\base\StaticInstanceInterface
isPrimaryKey() 傳回一個值,指示給定的一組屬性是否代表此模型的主鍵。 yii\db\ActiveRecordInterface
link() 建立兩個記錄之間的關聯。 yii\db\ActiveRecordInterface
populateRelation() 使用相關記錄填充指定的關聯。 yii\db\ActiveRecordInterface
primaryKey() 傳回此 AR 類別的主鍵名稱 yii\db\ActiveRecordInterface
save() 儲存目前的記錄。 yii\db\ActiveRecordInterface
setAttribute() 設定指定的屬性值。 yii\db\ActiveRecordInterface
unlink() 解除兩個記錄之間的關聯。 yii\db\ActiveRecordInterface
update() 將此 active record 的變更儲存到資料庫。 yii\db\ActiveRecordInterface
updateAll() 使用提供的屬性值和條件更新記錄。 yii\db\ActiveRecordInterface

方法詳細資訊

隱藏繼承的方法

attributes() 公開抽象方法

傳回記錄中所有屬性名稱的清單。

public abstract array attributes ( )
傳回 array

屬性名稱清單。

                public function attributes();

            
delete() 公開抽象方法

從資料庫刪除記錄。

public abstract integer|boolean delete ( )
傳回 integer|boolean

刪除的列數,或如果刪除因某些原因不成功,則返回 false。 請注意,即使刪除執行成功,刪除的列數也可能為 0。

                public function delete();

            
deleteAll() public abstract static method

使用提供的條件刪除記錄。

警告:如果您未指定任何條件,此方法將刪除表格中的所有列。

例如,刪除所有狀態為 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);

            
equals() public abstract method

傳回一個值,指示給定的 active record 是否與目前的 active record 相同。

兩個 new 記錄被認為不相等。

public abstract boolean equals ( $record )
$record static

要比較的記錄

傳回 boolean

兩個 active record 是否指向同一個資料庫表格中的同一列。

                public function equals($record);

            
find() public abstract static method

建立一個 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();

            
findAll() public abstract static method

傳回符合指定主鍵值或一組欄位值的 active record 模型清單。

此方法接受

  • 純量值(整數或字串):依單一主鍵值查詢,並傳回包含對應記錄的陣列(如果找不到則傳回空陣列)。
  • 非關聯陣列:依主鍵值清單查詢,並傳回對應的記錄(如果找不到則傳回空陣列)。 請注意,空條件將導致空結果,因為它將被解釋為搜尋主鍵,而不是空的 WHERE 條件。
  • 名稱-值配對的關聯陣列:依一組屬性值查詢,並傳回符合所有屬性值的記錄陣列(如果找不到則傳回空陣列)。 請注意,['id' => 1, 2] 被視為非關聯陣列。 欄位名稱僅限於 SQL DBMS 的目前記錄表格欄位,否則會過濾以限制為簡單的篩選條件。
  • yii\db\Expression:運算式將直接使用。 (@since 2.0.37)

此方法將自動呼叫 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);

            
findOne() public abstract static method

透過主鍵或欄位值陣列,傳回單一 active record 模型實例。

此方法接受

  • 純量值(整數或字串):依單一主鍵值查詢,並傳回對應的記錄(如果找不到則傳回 null)。
  • 非關聯陣列:依主鍵值清單查詢,並傳回第一個記錄(如果找不到則傳回 null)。
  • 名稱-值配對的關聯陣列:依一組屬性值查詢,並傳回符合所有屬性值的單一記錄(如果找不到則傳回 null)。 請注意,['id' => 1, 2] 被視為非關聯陣列。 欄位名稱僅限於 SQL DBMS 的目前記錄表格欄位,否則會過濾以限制為簡單的篩選條件。
  • yii\db\Expression:運算式將直接使用。 (@since 2.0.37)

此方法將自動呼叫 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 實例,或如果沒有任何符合項,則為 null

                public static function findOne($condition);

            
getAttribute() public abstract method

傳回指定的屬性值。

如果此記錄是查詢的結果,且屬性未載入,則將傳回 null

另請參閱 hasAttribute()

public abstract mixed getAttribute ( $name )
$name string

屬性名稱

傳回 mixed

屬性值。 如果未設定或不存在屬性,則為 null

                public function getAttribute($name);

            
getDb() public abstract static method

傳回此 AR 類別使用的連線。

public abstract static mixed getDb ( )
傳回 mixed

此 AR 類別使用的資料庫連線。

                public static function getDb();

            
getIsNewRecord() public abstract method

傳回一個值,指示目前的記錄是否為新記錄(尚未儲存到資料庫)。

public abstract boolean getIsNewRecord ( )
傳回 boolean

記錄是否為新的,且應在呼叫 save() 時插入。

                public function getIsNewRecord();

            
getOldPrimaryKey() public abstract method

傳回舊的主鍵值。

這指的是在執行 find 方法(例如 find()、findOne())後填入記錄的主鍵值。 即使主鍵屬性是以不同的值手動指派,此值仍保持不變。

public abstract mixed getOldPrimaryKey ( $asArray false )
$asArray boolean

是否將主鍵值作為陣列傳回。 如果為 true,則傳回值將是陣列,其中欄位名稱作為鍵,欄位值作為值。 如果此值為 false(預設值),則將為非複合主鍵傳回純量值。

傳回 mixed

舊的主鍵值。 如果主鍵是複合的或 $asArray 為 true,則傳回陣列(欄位名稱 => 欄位值)。 否則傳回字串(如果鍵值為 null,則將傳回 null)。

                public function getOldPrimaryKey($asArray = false);

            
getPrimaryKey() public abstract method

傳回主鍵值。

public abstract mixed getPrimaryKey ( $asArray false )
$asArray boolean

是否將主鍵值作為陣列傳回。 如果為 true,則傳回值將是陣列,其中屬性名稱作為鍵,屬性值作為值。 請注意,對於複合主鍵,無論此參數值為何,都將始終傳回陣列。

傳回 mixed

主鍵值。 如果主鍵是複合的或 $asArray 為 true,則傳回陣列(屬性名稱 => 屬性值)。 否則傳回字串(如果鍵值為 null,則將傳回 null)。

                public function getPrimaryKey($asArray = false);

            
getRelation() public abstract method

傳回具有指定名稱的關聯物件。

關聯是由 getter 方法定義的,該方法傳回實作 yii\db\ActiveQueryInterface 的物件(通常這會是關聯式 yii\db\ActiveQuery 物件)。 它可以在 ActiveRecord 類別本身或其行為之一中宣告。

public abstract yii\db\ActiveQueryInterface getRelation ( $name, $throwException true )
$name string

關聯名稱,例如透過 getOrders() 方法定義的關聯的 orders(區分大小寫)。

$throwException boolean

如果關聯不存在是否擲回例外。

傳回 yii\db\ActiveQueryInterface

關聯式查詢物件

                public function getRelation($name, $throwException = true);

            
hasAttribute() public abstract method

傳回一個值,指示記錄是否具有指定名稱的屬性。

public abstract boolean hasAttribute ( $name )
$name string

屬性的名稱

傳回 boolean

記錄是否具有指定名稱的屬性。

                public function hasAttribute($name);

            
insert() public abstract method

使用此記錄的屬性值,將記錄插入資料庫。

使用範例

$customer = new Customer;
$customer->name = $name;
$customer->email = $email;
$customer->insert();
public abstract boolean insert ( $runValidation true, $attributes null )
$runValidation boolean

是否在儲存記錄之前執行驗證(呼叫 validate())。 預設為 true。 如果驗證失敗,記錄將不會儲存到資料庫,且此方法將傳回 false

$attributes array|null

需要儲存的屬性清單。 預設為 null,表示將儲存從資料庫載入的所有屬性。

傳回 boolean

屬性是否有效且記錄是否插入成功。

                public function insert($runValidation = true, $attributes = null);

            
instance() public abstract static method

定義於: yii\base\StaticInstanceInterface::instance()

傳回靜態類別實例,可用於取得元資訊。

public abstract static static instance ( $refresh false )
$refresh boolean

即使已快取靜態實例,是否重新建立靜態實例。

傳回 static

類別實例。

                public static function instance($refresh = false);

            
isPrimaryKey() public abstract static method

傳回一個值,指示給定的一組屬性是否代表此模型的主鍵。

public abstract static boolean isPrimaryKey ( $keys )
$keys array

要檢查的屬性集

傳回 boolean

給定的屬性集是否代表此模型的主鍵

                public static function isPrimaryKey($keys);

            
link() public abstract method

建立兩個記錄之間的關聯。

透過將一個記錄中的外鍵值設定為另一個記錄中對應的主鍵值來建立關聯。 具有外鍵的記錄將儲存到資料庫中,而無需執行驗證。

如果關聯涉及連接表格,則將在連接表格中插入一個新列,其中包含來自兩個記錄的主鍵值。

此方法要求主鍵值不得為 null

public abstract void link ( $name, $model, $extraColumns = [] )
$name string

關聯的區分大小寫名稱,例如透過 getOrders() 方法定義的關聯的 orders

$model static

要與目前記錄連結的記錄。

$extraColumns array

要儲存到連接表格中的其他欄位值。 此參數僅對於涉及連接表格的關聯(即使用 yii\db\ActiveQueryInterface::via() 設定的關聯)才有意義。

populateRelation() public abstract method (available since version 2.0.8)

使用相關記錄填充指定的關聯。

請注意,此方法不會檢查關聯是否存在。

public abstract void populateRelation ( $name, $records )
$name string

關聯名稱,例如透過 getOrders() 方法定義的關聯的 orders(區分大小寫)。

$records yii\db\ActiveRecordInterface|array|null

要填入關聯的相關記錄。

                public function populateRelation($name, $records);

            
primaryKey() public abstract static method

傳回此 AR 類別的主鍵名稱

請注意,即使記錄只有單一主鍵,也應傳回陣列。

如需主鍵,請改為參閱 getPrimaryKey()

public abstract static string[] primaryKey ( )
傳回 string[]

此 AR 類別的主鍵名稱。

                public static function primaryKey();

            
save() public abstract method

儲存目前的記錄。

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())。 預設為 true。 如果驗證失敗,記錄將不會儲存到資料庫,且此方法將傳回 false

$attributeNames array|null

需要儲存的屬性名稱清單。 預設為 null,表示將儲存從資料庫載入的所有屬性。

傳回 boolean

儲存是否成功(即,未發生驗證錯誤)。

                public function save($runValidation = true, $attributeNames = null);

            
setAttribute() public abstract method

設定指定的屬性值。

另請參閱 hasAttribute()

public abstract void setAttribute ( $name, $value )
$name string

屬性名稱。

$value mixed

屬性值。

                public function setAttribute($name, $value);

            
unlink() public abstract method

解除兩個記錄之間的關聯。

如果 $delete 為 true,則會刪除具有關聯外鍵的記錄。 否則,外鍵將設定為 null,且記錄將在未驗證的情況下儲存。

public abstract void unlink ( $name, $model, $delete false )
$name string

關聯的區分大小寫名稱,例如透過 getOrders() 方法定義的關聯的 orders

$model static

要從目前記錄取消連結的模型。

$delete boolean

是否刪除包含外鍵的模型。 如果為 false,則模型的外鍵將設定為 null 並儲存。 如果為 true,則將刪除包含外鍵的模型。

update() public abstract method

將此 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())。 預設為 true。 如果驗證失敗,記錄將不會儲存到資料庫,且此方法將傳回 false

$attributeNames array|null

需要儲存的屬性清單。 預設為 null,表示將儲存從資料庫載入的所有屬性。

傳回 integer|boolean

受影響的列數,或如果驗證失敗或更新程序因其他原因停止,則返回 false。 請注意,即使更新執行成功,受影響的列數也可能為 0。

                public function update($runValidation = true, $attributeNames = null);

            
updateAll() public abstract static method

使用提供的屬性值和條件更新記錄。

例如,將所有狀態為 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);