PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Cakephp 3.0 Multiple tables

 February 22, 2022     cakephp, cakephp-3.0, database, mysql, php     No comments   

Issue

I'm sure I am just being an idiot, but can anyone help?, the following controller is only saving to the users table, not the child "Userapikey" table (All other data is auto built by Cakephp and MySQL):

$data = $this->request->data;
// Create the default access mask
$data[ 'userapikey' ] = [
    [ 'accessmask' => 22966271 ]
];

// Create a new entity in the DB and populate
$User = $this->Users->newEntity( $data, [ 'associated' => [ 'Userapikeys' ]  );

// Commit to the database
if( $this->Users->save( $User ) ) {
// Blah Blah send message etc.
}

UserTable.php has got the

$this->hasMany('Userapikeys', [ 'foreignKey' => 'user_id' ]);

Userapikeys.php has the

$this->belongsTo('Users', [ 'foreignKey' => 'user_id' ]);

If you need any more information I would be happy to provide


Solution

Eventually found that in the User.php entity please make sure the accessible included your "child" table eg.

protected $_accessible = [
    'username' => true,
    'password' => true,
    'user_api_keys' => true
];

and make sure it is the underscored version and plural



Answered By - B1scuit
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing