Welcome Guest, Not a member yet? Register   Sign In
Query builder return NULL on second iteration
#3

(04-10-2021, 08:24 PM)iRedds Wrote: This answer does not solve your problem. But he explains that with your code you will NEVER get a record with ID 67.

Every time you make the same request to the database
Code:
SELECT * FROM some_table WHERE post_id = 32 AND meta_key = 'category' AND meta_value = 1
And each time your query returns both records from the database, because both records match the query condition.

Next, you call the getRow() method with no arguments, which means it always returns the first value from the result.
That is, it will always be a record with ID 40.


The getRow() method will return null only in one case, when the query result is empty.

You can get the last SQL query through the BaseConnection class method:
PHP Code:
$this->db->getLastQuery() 

Hi, thanks for you answer, I will try to replicate the app behavior step by step. When the form is submitted, I sent the `meta['categories']` array which contains this:

PHP Code:
Array ( [0] => [1] => 

then, I call the `addMeta` function which execute the following:

PHP Code:
if (isset($meta['categories'])) {
            foreach ($meta['categories'] as $ctgId) {
                $this->postMeta->addPostMeta($postId'category'$ctgId);
            }
        

as you can see I check if the categories are actually setted, if so I will insert or update them using the method `addPostMeta` that contains this code:

PHP Code:
public function addPostMeta(int $postIdstring $keymixed $value)
    {
        // check meta existence
        $exists $this->getMetaByPostId($postId$key$valuetrue);
        $postMeta = new PostMetaEntity();

        echo (string)$this->db->getLastQuery();
        var_dump($exists);
        ... 

the problem here is inside `getMetaByPostId`:


PHP Code:
public function getMetaByPostId(int $postId$key ''$value ''$single false)
    {
        $builder $this->builder()
            ->select('*')
            ->where('post_id'$postId);

        if ($key != '') {
            $builder->where('meta_key'$key);

            if ($value) {
                $builder->where('meta_value'$value);
            }

            if ($single) {
                return $builder
                    
->get()
                    ->getRow();
            }

            return $builder
                
->get()
                ->getResultArray();
        }
        ... 

These are the meta available in the table for the postId 32:

[Image: 6j4trTG.png]

based on the code above I have this result for each iteration of the loop of `addMeta`:

first iteration

> $ctgId: 1
> getLastQuery() => `SELECT * FROM `blog_post_meta` WHERE `post_id` = 32 AND `meta_key` = 'category' AND `meta_value` = '1'`
> $exists: NULL
> PhpMyAdmin result:

[Image: lZKNpLY.png]


second iteration

> $ctgId: 3
> getLastQuery() => `SELECT * FROM `blog_post_meta` WHERE `post_id` = 32 AND `meta_key` = 'category' AND `meta_value` = '3'`
> $exists: NULL


> PhpMyAdmin result:

[Image: NIeveyv.png]
so, as you can see both record exists, but for some reason CodeIgniter returns NULL in both cases. I don't know if I'm doing something wrong actually, could you please help?
Thanks
Reply


Messages In This Thread
RE: Query builder return NULL on second iteration - by sfarzoso - 04-11-2021, 02:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB