• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
table with 2 primary keys update problem

#1
Hi!
I have a table with two primary keys, but no foreign keys and the table update is showing an error depending on the configuration of "protected $primaryKey".
If I remove "protected $primaryKey" it displays an error message but updates {else} it displays an error message and does not update.

Is it valid or possible to inform both primary keys?

Model:
Code:
<? php namespace App \ Models;

// 20200512

use CodeIgniter \ Model;

class PlayersAlbumsModel extends Model
{
     protected $table = 'players_albums';
     protected $primaryKey = 'player_id';    
    
     protected $TimeStamps = false;
     protected $useSoftDeletes = false;
     protected $allowedFields = ['player_id', 'album_id', 'pick'];

}


Pleas, any suggestion?
Reply

#2
Update:
When "$model->save" is used the problem occurs exactly as described above; if the option is $model->insert with or without the primary key, the error message occurs but the table is updated.

Controller:
Code:
public function artrec() {

        helper('form');

        $model = new PlayersAlbumsModel();

        $header = array (
            'icon' => 'codeigniter',
            'css' => '',           
            'title' => 'Atualização da tabela \'player_albuns\' - wdeda',
            'action' => '/ci4/search/allmedia/',
            'placeholder' => 'Pesquisar'
        );

        $rules = [
            'player_id'  => 'required',
            'album_id'  => 'required'
        ];

      if ($this->validate($rules)) {
          $model->insert([
              'player_id'  => $this->request->getVar('player_id'),
              'album_id'  => $this->request->getVar('album_id'),
              'pick'      => $this->request->getVar('pick')                   
          ]);
     

        echo view('templates/headerx', $header);
        echo view('utils/artec');
        echo view('templates/footerfb');

    } else {

        echo view('templates/headerx', $header);
        echo view('utils/artrec');
        echo view('templates/footerfb');
    }
    }


Attached Files
.pdf   insert with pkey off - update done.pdf (Size: 107.63 KB / Downloads: 3)
.pdf   insert with pkey on - update done.pdf (Size: 107.79 KB / Downloads: 2)
Reply

#3
In both screenshots, an exception occurs due to "forInvalidFile", when rendering one of the views:

PHP Code:
echo view('templates/headerx'$header);
echo 
view('utils/artrec');
echo 
view('templates/footerfb'); 

Perhaps one or more files were not found or are not where you indicated
Reply

#4
(05-12-2020, 07:14 PM)vitnibel Wrote: In both screenshots, an exception occurs due to "forInvalidFile", when rendering one of the views:

PHP Code:
echo view('templates/headerx'$header);
echo 
view('utils/artrec');
echo 
view('templates/footerfb'); 

Perhaps one or more files were not found or are not where you indicated

This was a great nonsense for those who were starting, 2015, of course, until today, things have not changed that much. The correct one would be a single primary key, id. The two fields, without attributes, are sufficient to make the relationship between player and record, as can be seen below.
Code:
public function getAlbums() {

        $request = \Config\Services::request();
        $uri = $request->uri;
        $id = $uri->getSegment(3);
        $db = db_connect();
        $sql = ("SELECT A.*, PA.* FROM albuns A, players_albuns PA
        WHERE PA.player_id=$id AND PA.album_id = A.id ORDER BY ano, posodr, album_id ASC");
        $query = $db->query($sql);

        return $query->getResult();
    }
The solution was to use a successful page, as in the CRUD tutorial, because in fact the update is done.
I should have already presented the solution but I was in the final steps of converting my site, localhost, from CI3 to CI4, just finished. I can't believe I did it. Big Grin
Thank you very much for your attention
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.