How to delete multiple rows in Yii 2
I have followed the link below in trying to achieve deleting multiple rows in Yii but so far the function I am using says it is not found by Yii, see the screenshot below.
This is the guide that I followed here in StackOverflow.
Delete multiple rows in YII2
This is the code that I am working on.
$keyfordeletion = ActiveSubject::find($clientid);
$keyfordeletion->CreateCommand()->deleteAll('subjectcontainer', 'clientid= ' . $clientid)->execute();
What's happening here is that keyfordeletion
will find all the values of clientid
in the activesubject
active record and will delete it through that function but it says the method is not found. So are there any ways to delete multiple rows in this case or solve the method function not found?
php yii2
add a comment |
I have followed the link below in trying to achieve deleting multiple rows in Yii but so far the function I am using says it is not found by Yii, see the screenshot below.
This is the guide that I followed here in StackOverflow.
Delete multiple rows in YII2
This is the code that I am working on.
$keyfordeletion = ActiveSubject::find($clientid);
$keyfordeletion->CreateCommand()->deleteAll('subjectcontainer', 'clientid= ' . $clientid)->execute();
What's happening here is that keyfordeletion
will find all the values of clientid
in the activesubject
active record and will delete it through that function but it says the method is not found. So are there any ways to delete multiple rows in this case or solve the method function not found?
php yii2
add a comment |
I have followed the link below in trying to achieve deleting multiple rows in Yii but so far the function I am using says it is not found by Yii, see the screenshot below.
This is the guide that I followed here in StackOverflow.
Delete multiple rows in YII2
This is the code that I am working on.
$keyfordeletion = ActiveSubject::find($clientid);
$keyfordeletion->CreateCommand()->deleteAll('subjectcontainer', 'clientid= ' . $clientid)->execute();
What's happening here is that keyfordeletion
will find all the values of clientid
in the activesubject
active record and will delete it through that function but it says the method is not found. So are there any ways to delete multiple rows in this case or solve the method function not found?
php yii2
I have followed the link below in trying to achieve deleting multiple rows in Yii but so far the function I am using says it is not found by Yii, see the screenshot below.
This is the guide that I followed here in StackOverflow.
Delete multiple rows in YII2
This is the code that I am working on.
$keyfordeletion = ActiveSubject::find($clientid);
$keyfordeletion->CreateCommand()->deleteAll('subjectcontainer', 'clientid= ' . $clientid)->execute();
What's happening here is that keyfordeletion
will find all the values of clientid
in the activesubject
active record and will delete it through that function but it says the method is not found. So are there any ways to delete multiple rows in this case or solve the method function not found?
php yii2
php yii2
edited Nov 19 '18 at 11:10
Mehran
181311
181311
asked Aug 21 '16 at 9:20
user3606724
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
There is static method deleteAll()
for this in ActiveRecord.
ActiveSubject::deleteAll(['clientid' => $clientid]);
This will delete all rows from ActiveSubject::tableName()
where clientid
is $clientid. $clientid can be single value or array of values.
add a comment |
you can delete one by one using foreach.
$deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
foreach($deleteall as $delete)
{
$delete->delete();
}
add a comment |
there is also a sql way to delete linked rows, with a foreign key and "on delete cascade" directive:
CREATE TABLE buildings (
building_no int(11) NOT NULL AUTO_INCREMENT,
building_name varchar(255) NOT NULL,
address varchar(355) NOT NULL,
PRIMARY KEY (building_no)
) ENGINE=InnoDB;
CREATE TABLE rooms (
room_no int(11) NOT NULL AUTO_INCREMENT,
room_name varchar(255) NOT NULL,
building_no int(11) NOT NULL,
PRIMARY KEY (room_no),
KEY building_no (building_no),
CONSTRAINT rooms_ibfk_1
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
) ENGINE=InnoDB
so if a row in "building" table is deleted, all related records from "rooms" table is deleted as well - without additional code.
for mysql this is only working with InnoDB engine.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39062702%2fhow-to-delete-multiple-rows-in-yii-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is static method deleteAll()
for this in ActiveRecord.
ActiveSubject::deleteAll(['clientid' => $clientid]);
This will delete all rows from ActiveSubject::tableName()
where clientid
is $clientid. $clientid can be single value or array of values.
add a comment |
There is static method deleteAll()
for this in ActiveRecord.
ActiveSubject::deleteAll(['clientid' => $clientid]);
This will delete all rows from ActiveSubject::tableName()
where clientid
is $clientid. $clientid can be single value or array of values.
add a comment |
There is static method deleteAll()
for this in ActiveRecord.
ActiveSubject::deleteAll(['clientid' => $clientid]);
This will delete all rows from ActiveSubject::tableName()
where clientid
is $clientid. $clientid can be single value or array of values.
There is static method deleteAll()
for this in ActiveRecord.
ActiveSubject::deleteAll(['clientid' => $clientid]);
This will delete all rows from ActiveSubject::tableName()
where clientid
is $clientid. $clientid can be single value or array of values.
answered Aug 21 '16 at 9:28
BizleyBizley
11.8k32035
11.8k32035
add a comment |
add a comment |
you can delete one by one using foreach.
$deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
foreach($deleteall as $delete)
{
$delete->delete();
}
add a comment |
you can delete one by one using foreach.
$deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
foreach($deleteall as $delete)
{
$delete->delete();
}
add a comment |
you can delete one by one using foreach.
$deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
foreach($deleteall as $delete)
{
$delete->delete();
}
you can delete one by one using foreach.
$deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
foreach($deleteall as $delete)
{
$delete->delete();
}
answered Aug 21 '16 at 12:40
Sanzeeb AryalSanzeeb Aryal
3,14511132
3,14511132
add a comment |
add a comment |
there is also a sql way to delete linked rows, with a foreign key and "on delete cascade" directive:
CREATE TABLE buildings (
building_no int(11) NOT NULL AUTO_INCREMENT,
building_name varchar(255) NOT NULL,
address varchar(355) NOT NULL,
PRIMARY KEY (building_no)
) ENGINE=InnoDB;
CREATE TABLE rooms (
room_no int(11) NOT NULL AUTO_INCREMENT,
room_name varchar(255) NOT NULL,
building_no int(11) NOT NULL,
PRIMARY KEY (room_no),
KEY building_no (building_no),
CONSTRAINT rooms_ibfk_1
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
) ENGINE=InnoDB
so if a row in "building" table is deleted, all related records from "rooms" table is deleted as well - without additional code.
for mysql this is only working with InnoDB engine.
add a comment |
there is also a sql way to delete linked rows, with a foreign key and "on delete cascade" directive:
CREATE TABLE buildings (
building_no int(11) NOT NULL AUTO_INCREMENT,
building_name varchar(255) NOT NULL,
address varchar(355) NOT NULL,
PRIMARY KEY (building_no)
) ENGINE=InnoDB;
CREATE TABLE rooms (
room_no int(11) NOT NULL AUTO_INCREMENT,
room_name varchar(255) NOT NULL,
building_no int(11) NOT NULL,
PRIMARY KEY (room_no),
KEY building_no (building_no),
CONSTRAINT rooms_ibfk_1
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
) ENGINE=InnoDB
so if a row in "building" table is deleted, all related records from "rooms" table is deleted as well - without additional code.
for mysql this is only working with InnoDB engine.
add a comment |
there is also a sql way to delete linked rows, with a foreign key and "on delete cascade" directive:
CREATE TABLE buildings (
building_no int(11) NOT NULL AUTO_INCREMENT,
building_name varchar(255) NOT NULL,
address varchar(355) NOT NULL,
PRIMARY KEY (building_no)
) ENGINE=InnoDB;
CREATE TABLE rooms (
room_no int(11) NOT NULL AUTO_INCREMENT,
room_name varchar(255) NOT NULL,
building_no int(11) NOT NULL,
PRIMARY KEY (room_no),
KEY building_no (building_no),
CONSTRAINT rooms_ibfk_1
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
) ENGINE=InnoDB
so if a row in "building" table is deleted, all related records from "rooms" table is deleted as well - without additional code.
for mysql this is only working with InnoDB engine.
there is also a sql way to delete linked rows, with a foreign key and "on delete cascade" directive:
CREATE TABLE buildings (
building_no int(11) NOT NULL AUTO_INCREMENT,
building_name varchar(255) NOT NULL,
address varchar(355) NOT NULL,
PRIMARY KEY (building_no)
) ENGINE=InnoDB;
CREATE TABLE rooms (
room_no int(11) NOT NULL AUTO_INCREMENT,
room_name varchar(255) NOT NULL,
building_no int(11) NOT NULL,
PRIMARY KEY (room_no),
KEY building_no (building_no),
CONSTRAINT rooms_ibfk_1
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
) ENGINE=InnoDB
so if a row in "building" table is deleted, all related records from "rooms" table is deleted as well - without additional code.
for mysql this is only working with InnoDB engine.
answered Aug 22 '16 at 8:23
e-franke-frank
484511
484511
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39062702%2fhow-to-delete-multiple-rows-in-yii-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown