How to delete multiple rows in Yii 2












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.
enter image description here



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?










share|improve this question





























    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.
    enter image description here



    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?










    share|improve this question



























      2












      2








      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.
      enter image description here



      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?










      share|improve this question
















      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.
      enter image description here



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 11:10









      Mehran

      181311




      181311










      asked Aug 21 '16 at 9:20







      user3606724































          3 Answers
          3






          active

          oldest

          votes


















          7














          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.






          share|improve this answer































            0














            you can delete one by one using foreach.



             $deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
            foreach($deleteall as $delete)
            {
            $delete->delete();
            }





            share|improve this answer































              0














              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.






              share|improve this answer























                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
                });


                }
                });














                draft saved

                draft discarded


















                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









                7














                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.






                share|improve this answer




























                  7














                  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.






                  share|improve this answer


























                    7












                    7








                    7







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 21 '16 at 9:28









                    BizleyBizley

                    11.8k32035




                    11.8k32035

























                        0














                        you can delete one by one using foreach.



                         $deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
                        foreach($deleteall as $delete)
                        {
                        $delete->delete();
                        }





                        share|improve this answer




























                          0














                          you can delete one by one using foreach.



                           $deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
                          foreach($deleteall as $delete)
                          {
                          $delete->delete();
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            you can delete one by one using foreach.



                             $deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
                            foreach($deleteall as $delete)
                            {
                            $delete->delete();
                            }





                            share|improve this answer













                            you can delete one by one using foreach.



                             $deleteall = ActiveSubject::where(['clientid'=>$clientid])->all();
                            foreach($deleteall as $delete)
                            {
                            $delete->delete();
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 21 '16 at 12:40









                            Sanzeeb AryalSanzeeb Aryal

                            3,14511132




                            3,14511132























                                0














                                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.






                                share|improve this answer




























                                  0














                                  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.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    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.






                                    share|improve this answer













                                    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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 22 '16 at 8:23









                                    e-franke-frank

                                    484511




                                    484511






























                                        draft saved

                                        draft discarded




















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        How to change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents