I’m unable to do INNER JOIN in cakephp

Asked

Viewed 88 times

1

All right, guys? I’m using cakephp to make a system that interacts with some database data from a Wordpress site

however every time I try to perform an Inner Join cake query shows me this error:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Wppostmeta.meta_key' in 'Where clause'

My Controller code is this:

$join = array(
        array(
            'table' => 'WpPostmeta',
            'alias' => 'wp-postmeta',
            'type' =>  'INNER',
            'conditions'=> array('WpPosts.ID = WpPostmeta.post_id',)
        )
    );
    $wpPosts  =  $this->RequestProduct->WpPosts->find('all',array(
        'joins' => $join,
        'conditions' => array('WpPostmeta.meta_key' => '_stock_status','WpPostmeta.meta_value' => 'instock') 
    )
    );

Because the cake is not recognizing the Wppostmeta column if I put it in the $Join variable???

NOTE: I looked at the debug and really the sql generated by cake is not returning these columns in select...

This is my code from the Wppostmetatable class:

public function initialize(array $config)
    {

        parent::initialize($config);

        $this->setTable('wp_postmeta');
        $this->setDisplayField('meta_id');
        $this->setPrimaryKey('meta_id');

        $this->belongsTo('WpPosts', [
            'foreignKey' => 'post_id',
            'joinType' => 'INNER'
        ]);
    }

And that’s my code from the Wppoststable class:

public function initialize(array $config)
    {

        parent::initialize($config);

        $this->setTable('wp_posts');
        $this->setDisplayField('ID');
        $this->setPrimaryKey('ID');

        $this->hasMany('WpPostmeta', [
             'foreignKey' => 'post_id'
        ]);
    }

That’s the Sql I wanted the cake to make:

    SELECT
     wp_posts.*,
     wp_postmeta.*
FROM
     `wp_posts` wp_posts INNER JOIN `wp_postmeta` wp_postmeta ON wp_posts.`ID` = wp_postmeta.`post_id`
WHERE
     wp_postmeta.`meta_key` = '_stock_status'
 AND wp_postmeta.`meta_value` = 'instock'

I also tried this way which is what the cakephp documentation recommends (but gives the same error...):

$wpPosts = $this->RequestProduct->WpPosts->find('all', [
        'contain' => [
            'WpPostmeta'
        ],
        'conditions' => [
            'WpPostmeta.meta_key' => '_stock_status',
            'WpPostmeta.meta_value' => 'instock'
        ]
]);

cake does not recognize 'Wppostmeta.meta_key' and 'Wppostmeta.meta_value' as part of Join...

I managed to solve my problem with that gambit:

$WpPostmetaTable = TableRegistry::get('WpPostmeta');
$WpPostmeta = $WpPostmetaTable->find('all', array(
   'conditions'=>array(
      'WpPostmeta.meta_key' => '_stock_status',
      'WpPostmeta.meta_value' => 'instock'
   )
));
$i = 0;
$wpPosts = array();
foreach ($WpPostmeta as $WpPostmetaArray){
    $wpPosts[$i] = $this->RequestProduct->WpPosts->get($WpPostmetaArray['post_id'], [
        'contain' => []
    ]);
    $WpPostmetaTemp = $WpPostmetaTable->find('all', array('conditions'=>array(
        'WpPostmeta.post_id' => $wpPosts[$i]['ID'])
    ));
    $wpPosts[$i]['wppostmeta'] = $WpPostmetaTemp;
    $i += 1;
}
  • wouldn’t it be "wp_postmeta" instead of "wp-postmeta" ? Why are you defining an alias if you’re not using it?

  • Even if I change the name and use the alias it still gives the same error... Error: Unknown column 'wp_postmeta.meta_key' in 'Where clause'

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.