Commit 46247939 by Alexander Makarov

Merge branch 'unlinkAll' of github.com:NmDimas/yii2 into NmDimas-unlinkAll

Conflicts: framework/CHANGELOG.md
parents ceb2689f 95d2b29b
......@@ -60,7 +60,8 @@ Yii Framework 2 Change Log
- Enh #3328: `BaseMailer` generates better text body from html body (armab)
- Enh #3380: Allow `value` in `defaultValueValidator` to be a closure (Alex-Code)
- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v)
- Enh #3518: `yii\helpers\Html::encode()` now replaces invalid code sequences with "?" (DaSourcerer)
- Enh #3518: `yii\helpers\Html::encode()` now replaces invalid code sequences with "�" (DaSourcerer)
- Enh #3520 Added Unlink all relationship in current model (NmDimas)
- Enh #3521: Added `yii\filters\HttpCache::sessionCacheLimiter` (qiangxue)
- Enh #3542: Removed requirement to specify `extensions` in application config (samdark)
- Enh #3562: Adding rotateByCopy to yii\log\FileTarget (pawzar)
......
......@@ -1284,6 +1284,67 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
}
/**
* Destroys the relationship in current model.
*
* The model with the foreign key of the relationship will be deleted if `$delete` is true.
* Otherwise, the foreign key will be set null and the model will be saved without validation.
*
* Note to destroy the relationship without removing records make sure your keys can be set to null
*
* @param string $name the case sensitive name of the relationship.
* @param boolean $delete whether to delete the model that contains the foreign key.
*/
public function unlinkAll($name, $delete = false)
{
$relation = $this->getRelation($name);
/** @var Command $command */
$command = static::getDb()->createCommand();
$columns = [];
$condition = [];
if (empty($relation->via)) {
/** @var $viaClass \yii\db\ActiveRecord */
$viaClass = $relation->modelClass;
if ($delete) {
foreach ($relation->link as $a => $b) {
$condition[$a] = $this->$b;
}
$command->delete($viaClass::tableName(), $condition)->execute();
} else {
foreach ($relation->link as $a => $b) {
$columns[$a] = null;
$condition[$a] = $this->$b;
}
$command->update($viaClass::tableName(), $columns, $condition)->execute();
}
} else {
$viaTable = $viaTable = reset($relation->via->from);
/** @var ActiveQuery $viaRelation */
$viaRelation = $relation->via;
if ($delete) {
foreach ($viaRelation->link as $a => $b) {
$condition[$a] = $this->$b;
}
$command->delete($viaTable, $condition)->execute();
} else {
foreach ($viaRelation->link as $a => $b) {
$columns[$a] = null;
$condition[$a] = $this->$b;
}
$command->update($viaTable, $columns,$condition)->execute();
}
}
if (!$relation->multiple) {
unset($this->_related[$name]);
} elseif (isset($this->_related[$name])) {
/** @var ActiveRecordInterface $b */
foreach (array_keys($this->_related[$name]) as $a) {
unset($this->_related[$name][$a]);
}
}
}
/**
* @param array $link
* @param ActiveRecordInterface $foreignModel
* @param ActiveRecordInterface $primaryModel
......
......@@ -36,6 +36,10 @@ class Customer extends ActiveRecord
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');
}
public function getOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('id');
}
public function getOrders2()
{
......
......@@ -40,6 +40,12 @@ class Order extends ActiveRecord
})->orderBy('item.id');
}
public function getItemsWithNullFK()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->viaTable('order_item_with_null_fk', ['order_id' => 'id']);
}
public function getItemsInOrder1()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......@@ -63,6 +69,13 @@ class Order extends ActiveRecord
->where(['category_id' => 1]);
}
public function getBooksWithNullFK()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->viaTable('order_item_with_null_fk', ['order_id' => 'id'])
->where(['category_id' => 1]);
}
public function getBooks2()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......
<?php
namespace yiiunit\data\ar;
/**
* Class OrderItem
*
* @property integer $order_id
* @property integer $item_id
* @property integer $quantity
* @property string $subtotal
*/
class OrderItemWithNullFK extends ActiveRecord
{
public static function tableName()
{
return 'order_item_with_null_fk';
}
}
<?php
namespace yiiunit\data\ar;
/**
* Class Order
*
* @property integer $id
* @property integer $customer_id
* @property integer $created_at
* @property string $total
*/
class OrderWithNullFK extends ActiveRecord
{
public static function tableName()
{
return 'order_with_null_fk';
}
}
......@@ -5,8 +5,10 @@
DROP TABLE IF EXISTS `composite_fk`;
DROP TABLE IF EXISTS `order_item`;
DROP TABLE IF EXISTS `order_item_with_null_fk`;
DROP TABLE IF EXISTS `item`;
DROP TABLE IF EXISTS `order`;
DROP TABLE IF EXISTS `order_with_null_fk`;
DROP TABLE IF EXISTS `category`;
DROP TABLE IF EXISTS `customer`;
DROP TABLE IF EXISTS `profile`;
......@@ -60,6 +62,14 @@ CREATE TABLE `order` (
CONSTRAINT `FK_order_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE
);
CREATE TABLE `order_with_null_fk` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11),
`created_at` int(11) NOT NULL,
`total` decimal(10,0) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `order_item` (
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
......@@ -70,6 +80,13 @@ CREATE TABLE `order_item` (
CONSTRAINT `FK_order_item_item_id` FOREIGN KEY (`item_id`) REFERENCES `item` (`id`) ON DELETE CASCADE
);
CREATE TABLE `order_item_with_null_fk` (
`order_id` int(11),
`item_id` int(11),
`quantity` int(11) NOT NULL,
`subtotal` decimal(10,0) NOT NULL
);
CREATE TABLE null_values (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`var1` INT NULL,
......@@ -124,9 +141,20 @@ INSERT INTO `order` (customer_id, created_at, total) VALUES (1, 1325282384, 110.
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
IF OBJECT_ID('[dbo].[order_item]', 'U') IS NOT NULL DROP TABLE [dbo].[order_item];
IF OBJECT_ID('[dbo].[order_item_with_null_fk]', 'U') IS NOT NULL DROP TABLE [dbo].[order_item_with_null_fk];
IF OBJECT_ID('[dbo].[item]', 'U') IS NOT NULL DROP TABLE [dbo].[item];
IF OBJECT_ID('[dbo].[order]', 'U') IS NOT NULL DROP TABLE [dbo].[order];
IF OBJECT_ID('[dbo].[order_with_null_fk]', 'U') IS NOT NULL DROP TABLE [dbo].[order_with_null_fk];
IF OBJECT_ID('[dbo].[category]', 'U') IS NOT NULL DROP TABLE [dbo].[category];
IF OBJECT_ID('[dbo].[customer]', 'U') IS NOT NULL DROP TABLE [dbo].[customer];
IF OBJECT_ID('[dbo].[profile]', 'U') IS NOT NULL DROP TABLE [dbo].[profile];
......@@ -54,6 +56,13 @@ CREATE TABLE [dbo].[order] (
) ON [PRIMARY]
);
CREATE TABLE [dbo].[order_with_null_fk] (
[id] [int] IDENTITY(1,1) NOT NULL,
[customer_id] [int] ,
[created_at] [int] NOT NULL,
[total] [decimal](10,0) NOT NULL
);
CREATE TABLE [dbo].[order_item] (
[order_id] [int] NOT NULL,
[item_id] [int] NOT NULL,
......@@ -63,6 +72,12 @@ CREATE TABLE [dbo].[order_item] (
[order_id] ASC,
[item_id] ASC
) ON [PRIMARY]
);CREATE TABLE [dbo].[order_item_with_null_fk] (
[order_id] [int],
[item_id] [int],
[quantity] [int] NOT NULL,
[subtotal] [decimal](10,0) NOT NULL
);
CREATE TABLE [dbo].[null_values] (
......@@ -109,9 +124,20 @@ INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (1, 1325
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (2, 1325334482, 33.0);
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (2, 1325502201, 40.0);
INSERT INTO [dbo].[order_with_null_fk] ([customer_id], [created_at], [total]) VALUES (1, 1325282384, 110.0);
INSERT INTO [dbo].[order_with_null_fk] ([customer_id], [created_at], [total]) VALUES (2, 1325334482, 33.0);
INSERT INTO [dbo].[order_with_null_fk] ([customer_id], [created_at], [total]) VALUES (2, 1325502201, 40.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 1, 1, 30.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 2, 2, 40.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 4, 1, 10.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 5, 1, 15.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 3, 1, 8.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (3, 2, 1, 40.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 1, 1, 30.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 2, 2, 40.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 4, 1, 10.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 5, 1, 15.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 3, 1, 8.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (3, 2, 1, 40.0);
......@@ -5,8 +5,10 @@
DROP TABLE IF EXISTS `composite_fk` CASCADE;
DROP TABLE IF EXISTS `order_item` CASCADE;
DROP TABLE IF EXISTS `order_item_with_null_fk` CASCADE;
DROP TABLE IF EXISTS `item` CASCADE;
DROP TABLE IF EXISTS `order` CASCADE;
DROP TABLE IF EXISTS `order_with_null_fk` CASCADE;
DROP TABLE IF EXISTS `category` CASCADE;
DROP TABLE IF EXISTS `customer` CASCADE;
DROP TABLE IF EXISTS `profile` CASCADE;
......@@ -61,6 +63,14 @@ CREATE TABLE `order` (
CONSTRAINT `FK_order_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_with_null_fk` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11),
`created_at` int(11) NOT NULL,
`total` decimal(10,0) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_item` (
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
......@@ -72,6 +82,14 @@ CREATE TABLE `order_item` (
CONSTRAINT `FK_order_item_item_id` FOREIGN KEY (`item_id`) REFERENCES `item` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_item_with_null_fk` (
`order_id` int(11),
`item_id` int(11),
`quantity` int(11) NOT NULL,
`subtotal` decimal(10,0) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `composite_fk` (
`id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
......@@ -124,6 +142,10 @@ INSERT INTO `order` (customer_id, created_at, total) VALUES (1, 1325282384, 110.
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
......@@ -131,6 +153,13 @@ INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
/**
* (MySQL-)Database Schema for validator tests
......
......@@ -6,7 +6,9 @@
DROP TABLE IF EXISTS "order_item" CASCADE;
DROP TABLE IF EXISTS "item" CASCADE;
DROP TABLE IF EXISTS "order_item_with_null_fk" CASCADE;
DROP TABLE IF EXISTS "order" CASCADE;
DROP TABLE IF EXISTS "order_with_null_fk" CASCADE;
DROP TABLE IF EXISTS "category" CASCADE;
DROP TABLE IF EXISTS "customer" CASCADE;
DROP TABLE IF EXISTS "profile" CASCADE;
......@@ -54,6 +56,13 @@ CREATE TABLE "order" (
total decimal(10,0) NOT NULL
);
CREATE TABLE "order_with_null_fk" (
id serial not null primary key,
customer_id integer,
created_at integer NOT NULL,
total decimal(10,0) NOT NULL
);
CREATE TABLE "order_item" (
order_id integer NOT NULL references "order"(id) on UPDATE CASCADE on DELETE CASCADE,
item_id integer NOT NULL references "item"(id) on UPDATE CASCADE on DELETE CASCADE,
......@@ -62,6 +71,13 @@ CREATE TABLE "order_item" (
PRIMARY KEY (order_id,item_id)
);
CREATE TABLE "order_item_with_null_fk" (
order_id integer,
item_id integer,
quantity integer NOT NULL,
subtotal decimal(10,0) NOT NULL
);
CREATE TABLE "null_values" (
id INT NOT NULL,
var1 INT NULL,
......@@ -106,6 +122,10 @@ INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
......@@ -113,6 +133,13 @@ INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
/**
* (Postgres-)Database Schema for validator tests
*/
......
......@@ -5,8 +5,10 @@
DROP TABLE IF EXISTS "composite_fk";
DROP TABLE IF EXISTS "order_item";
DROP TABLE IF EXISTS "order_item_with_null_fk";
DROP TABLE IF EXISTS "item";
DROP TABLE IF EXISTS "order";
DROP TABLE IF EXISTS "order_with_null_fk";
DROP TABLE IF EXISTS "category";
DROP TABLE IF EXISTS "customer";
DROP TABLE IF EXISTS "profile";
......@@ -50,6 +52,14 @@ CREATE TABLE "order" (
PRIMARY KEY (id)
);
CREATE TABLE "order_with_null_fk" (
id INTEGER NOT NULL,
customer_id INTEGER,
created_at INTEGER NOT NULL,
total decimal(10,0) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE "order_item" (
order_id INTEGER NOT NULL,
item_id INTEGER NOT NULL,
......@@ -58,6 +68,13 @@ CREATE TABLE "order_item" (
PRIMARY KEY (order_id, item_id)
);
CREATE TABLE "order_item_with_null_fk" (
order_id INTEGER,
item_id INTEGER,
quantity INTEGER NOT NULL,
subtotal decimal(10,0) NOT NULL,
);
CREATE TABLE "composite_fk" (
id int(11) NOT NULL,
order_id int(11) NOT NULL,
......@@ -109,6 +126,10 @@ INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
......@@ -116,6 +137,13 @@ INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
/**
* (SqLite-)Database Schema for validator tests
*/
......
......@@ -678,24 +678,95 @@ trait ActiveRecordTestTrait
$customerClass = $this->getCustomerClass();
/** @var \yii\db\ActiveRecordInterface $orderClass */
$orderClass = $this->getOrderClass();
/** @var \yii\db\ActiveRecordInterface $orderWithNullFKClass */
$orderWithNullFKClass = $this->getOrderWithNullFKClass();
/** @var \yii\db\ActiveRecordInterface $orderItemsWithNullFKClass */
$orderItemsWithNullFKClass = $this->getOrderIteWithNullFKmClass();
/** @var TestCase|ActiveRecordTestTrait $this */
// has many
// has many without delete
$customer = $customerClass::findOne(2);
$this->assertEquals(2, count($customer->ordersWithNullFK));
$customer->unlink('ordersWithNullFK', $customer->ordersWithNullFK[1], false);
$this->assertEquals(1, count($customer->ordersWithNullFK));
$orderWithNullFK = $orderWithNullFKClass::findOne(3);
$this->assertEquals(3,$orderWithNullFK->id);
$this->assertNull($orderWithNullFK->customer_id);
// has many with delete
$customer = $customerClass::findOne(2);
$this->assertEquals(2, count($customer->orders));
$customer->unlink('orders', $customer->orders[1], true);
$this->afterSave();
$this->assertEquals(1, count($customer->orders));
$this->assertNull($orderClass::findOne(3));
// via model
// via model with delete
$order = $orderClass::findOne(2);
$this->assertEquals(3, count($order->items));
$this->assertEquals(3, count($order->orderItems));
$order->unlink('items', $order->items[2], true);
$this->afterSave();
$this->assertEquals(2, count($order->items));
$this->assertEquals(2, count($order->orderItems));
// via model without delete
$this->assertEquals(3, count($order->itemsWithNullFK));
$order->unlink('itemsWithNullFK', $order->itemsWithNullFK[2], false);
$this->assertEquals(2, count($order->itemsWithNullFK));
$this->assertEquals(2, count($order->orderItems));
}
public function testUnlinkAll()
{
/** @var \yii\db\ActiveRecordInterface $customerClass */
$customerClass = $this->getCustomerClass();
/** @var \yii\db\ActiveRecordInterface $orderClass */
$orderClass = $this->getOrderClass();
$orderWithNullFKClass = $this->getOrderWithNullFKClass();
/** @var \yii\db\ActiveRecordInterface $orderItemsWithNullFKClass */
$orderItemsWithNullFKClass = $this->getOrderIteWithNullFKmClass();
/** @var TestCase|ActiveRecordTestTrait $this */
// has many with delete
$customer = $customerClass::findOne(2);
$this->assertEquals(2, count($customer->orders));
$customer->unlinkAll('orders', true);
$this->assertEquals(0, count($customer->orders));
$this->assertNull($orderClass::findOne(2));
$this->assertNull($orderClass::findOne(3));
// has many without delete
$customer = $customerClass::findOne(2);
$this->assertEquals(2, count($customer->ordersWithNullFK));
$customer->unlinkAll('ordersWithNullFK', false);
$this->assertEquals(0, count($customer->ordersWithNullFK));
$this->assertEquals(2,$orderWithNullFKClass::find()->where('(id=2 OR id=3) AND customer_id IS NULL')->count());
// via model with delete
/** @var Order $order */
$order = $orderClass::findOne(1);
$this->assertEquals(2, count($order->books));
$order->unlinkAll('books', true);
$this->assertEquals(0, count($order->books));
// via model without delete
$books = $order->booksWithNullFK;
$this->assertEquals(2, count($books));
$order->unlinkAll('booksWithNullFK',false);
$this->assertEquals(2,$orderItemsWithNullFKClass::find()->where('(item_id=1 OR item_id=2) AND order_id IS NULL')->count());
}
public static $afterSaveNewRecord;
......
......@@ -7,6 +7,8 @@ use yiiunit\data\ar\NullValues;
use yiiunit\data\ar\OrderItem;
use yiiunit\data\ar\Order;
use yiiunit\data\ar\Item;
use yiiunit\data\ar\OrderItemWithNullFK;
use yiiunit\data\ar\OrderWithNullFK;
use yiiunit\data\ar\Profile;
use yiiunit\data\ar\Type;
use yiiunit\framework\ar\ActiveRecordTestTrait;
......@@ -46,6 +48,15 @@ class ActiveRecordTest extends DatabaseTestCase
return OrderItem::className();
}
public function getOrderWithNullFKClass()
{
return OrderWithNullFK::className();
}
public function getOrderIteWithNullFKmClass()
{
return OrderItemWithNullFK::className();
}
public function testCustomColumns()
{
// find custom column
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment