Multiple primary key from MySQL view

6338

db.png
Hi,

First of all, congratulations on this superb piece of software which I’m using to get myself into the MVC (and OO PHP) world.

To give myself a tough beginning I’m trying to develop a historical database (world cultures and related bibliography), part of its ER diagram you may find attached to this post.

The central piece is table Costumbre which has a many-to-many relationship to table Bibliografia by means of table auxiliar_costumbre_bibliografia which in turn also holds additional fields as "observaciones" and "pagina_inicial" (starting page) as the same tradition referred to in the same book may appear in different pages (so three primary keys are defined for this intermediate table).

First question:

Is this schema a proper base to be managed by Yii2? I would like to use a common form to enter data for Costumbres and complementary fields in auxiliar_costumbre_bibliografia so I’m hesitating about this way of planning things.

Second question:

In order to speed queries up I created a MySQL view spanning all these tables to show some of the most relevant fields. Then I created a VistaCostumbreBibliografia and VistaCostumbreBibliografiaSearch models to handle this view from CostumbreController and they work fine except when choosing to view a particular model in DetailView. As per the Yii2 documentation, a primaryKey function is needed in the VistaCostumbreBibliografia model so I did:


    public static function primaryKey()

    {

        return ['vcostumbre_id', 'vbibliografia_id', 'vpagina_inicial'];

    }

which should return an array with the three primary keys of the intermediate table auxiliar_costumbre_bibliografia and feed it into the Gii-generated CostumbreController, specifically:


     public function actionView($id)

    {

        return $this->render('view', [

            'model' => $this->findModel($id),

        ]);

    }

.....


    protected function findModel($id)

    {

        if (($model = VistaCostumbreBibliografia::findOne($id)) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

    }

However it throws a Bad Request (mssing ‘id’). What should I do about this?

Thanks in advance for any help.

Antonio