CRUD many to many in Yii2

I have 3 table

post - id, title

category - id, title

category_post - category_id, post_id

How to create, update, delete with relation many to many. I can’t found any tutorial about it.

I hope the following wiki article will be a little help.

http://www.yiiframework.com/wiki/836/how-to-use-listbox-and-checkboxlist/

It covers how to create/update/delete the many to many relations to categories from a post.

Okay, thank you!

I will try it before I has any question.

Hi all

I am learning Yii2 and feel that the many to many relations are the key which I need to do something useful with this framework, but after spending a bit of time on this, I feel that i cannot think the way it is supposed to in yii2.

I don’t understand the example in www.yiiframework.com/wiki/836/how-to-use-listbox-and-checkboxlist/

I cannot get it to work.

The steps I’ve made are:

Created tables as in example:




create table post (id serial, title varchar(255), content text, pub boolean);

create table category (id serial, name varchar(255));

create table postcategory (post_id bigint unsigned not null, category_id bigint unsigned not null, foreign key (post_id) references post(id) on delete cascade, foreign key(category_id) references category(id) on delete cascade);



Created models and controllers, you can see each change I’ve made on github: /cpasqualini/learning-yii2

Error without use app\models\PostWithCategories;

Previous to 079c4d59a3135e2229438b41b2a4954388f74929




Error: Class 'app\controllers\PostWithCategories' not found in [path]/controllers/PostController.php:66

Stack trace:

#0 [internal function]: app\controllers\PostController->actionCreate()

#1 [path]/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#2 [path]/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#3 [path]/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)

#4 [path]/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('post/create', Array)

#5 [path]/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))

#6 [path]/web/index.php(12): yii\base\Application->run()

#7 {main}



Error with use app\models\PostWithCategories;

After 079c4d59a3135e2229438b41b2a4954388f74929




ParseError: syntax error, unexpected '$category_ids' (T_VARIABLE), expecting function (T_FUNCTION) in [path]/models/PostWithCategories.php:12

Stack trace:

#0 [internal function]: yii\BaseYii::autoload('app\\models\\Post...')

#1 [path]/controllers/PostController.php(67): spl_autoload_call('app\\models\\Post...')

#2 [internal function]: app\controllers\PostController->actionCreate()

#3 [path]/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#4 [path]/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#5 [path]/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)

#6 [path]/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('post/create', Array)

#7 [path]/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))

#8 [path]/web/index.php(12): yii\base\Application->run()

#9 {main}



Commenting the line (and adding ArrayHelpers to model), it does not show any error.

Previous to commit aa5e974346a13bb35607cc9eec5c8d4504b03eed

Then, adding the category_ids field to the form:

commit aa5e974346a13bb35607cc9eec5c8d4504b03eed




yii\base\ErrorException: Undefined variable: categories in [path]/views/post/_form.php:22

Stack trace:

#0 [path]/views/post/_form.php(22): yii\base\ErrorHandler->handleError(8, 'Undefined varia...', '/home/charly/ww...', 22, Array)

#1 [path]/vendor/yiisoft/yii2/base/View.php(332): require('/home/charly/ww...')

#2 [path]/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile('/home/charly/ww...', Array)

#3 [path]/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile('/home/charly/ww...', Array, NULL)

#4 [path]/views/post/create.php(18): yii\base\View->render('_form', Array)

#5 [path]/vendor/yiisoft/yii2/base/View.php(332): require('/home/charly/ww...')

#6 [path]/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile('/home/charly/ww...', Array)

#7 [path]/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile('/home/charly/ww...', Array, Object(app\controllers\PostController))

#8 [path]/vendor/yiisoft/yii2/base/Controller.php(384): yii\base\View->render('create', Array, Object(app\controllers\PostController))

#9 [path]/controllers/PostController.php(77): yii\base\Controller->render('create', Array)

#10 [internal function]: app\controllers\PostController->actionCreate()

#11 [path]/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#12 [path]/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#13 [path]/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)

#14 [path]/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('post/create', Array)

#15 [path]/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))

#16 [path]/web/index.php(12): yii\base\Application->run()

#17 {main}



Which makes sense, as I commented out the declaration of that array.

It is clear to me that I am doung something wrong, but cannot understand what. I would thank any hint on this

Thanks!

My guess: declare visibility

http://php.net/manual/en/language.oop5.visibility.php

Thanks!

Had made two more commits:

and then

but now I have a new error, and I’m stuck again:


Error: Class 'app\controllers\Category' not found in [path]/controllers/PostController.php:78

Stack trace:

#0 [internal function]: app\controllers\PostController->actionCreate()

#1 [path]/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#2 [path]/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#3 [path]/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)

#4 [path]/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('post/create', Array)

#5 [path]/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))

#6 [path]/web/index.php(12): yii\base\Application->run()

#7 {main}



Thanks!

http://php.net/manual/en/language.namespaces.importing.php

app\controllers\Category app\models\Category