Welcome Guest, Not a member yet? Register   Sign In
Bug: Double use where and etc
#1

(This post was last modified: 12-08-2019, 11:41 AM by XTAZ.)

Hello!

Model have paginate method, I needs extends it in my model.

I extends countAllResults() and findAll()
PHP Code:
    public function paginateByUser($user_id nullint $perPage 20string $group 'default'int $page 0)
    {
        
// Get the necessary parts.
        
$page $page >= $page : (ctype_digit($_GET['page'] ?? '') && $_GET['page'] > $_GET['page'] : 1);

        
$total $this->countAllResultsByUser($user_idfalse);

        
// Store it in the Pager library so it can be
        // paginated in the views.
        
$pager       = \Config\Services::pager();
        
$this->pager $pager->store($group$page$perPage$total);

        
$offset = ($page 1) * $perPage;

        return 
$this->findAllByUser($user_id$perPage$offset);
    }
    
    public function 
findAllByUser($user_id nullint $limit 0int $offset 0)
    {
        if (
is_null($user_id))
            
$user_id = \Config\Services::auth()->getUserID();
        
        
$this->select($this->table.'.*');
        
$this->join('users_projects up''up.project_id = '.$this->table.'.project_id AND up.deleted_at IS NULL AND up.user_id = '.intval($user_id));
        
        return 
parent::findAll($limit$offset);
    }
    
    public function 
countAllResultsByUser($user_id nullbool $reset truebool $test false)
    {
        if (
is_null($user_id))
            
$user_id = \Config\Services::auth()->getUserID();
        
        
$this->join('users_projects up''up.project_id = '.$this->table.'.project_id AND up.deleted_at IS NULL AND up.user_id = '.intval($user_id));
        
        return 
parent::countAllResults($reset$test);
    } 
And I get db error: Not unique table/alias: 'up'. It because join added in countAllResultsByUser() and findAllByUser(). Same bug with deleted_at in paginate (two deleted_at where).

I cant remove join from one function, because it can be used independently.

Generated query:
Code:
SELECT `my_table`.*
FROM `my_table`
JOIN `users_projects` `up` ON `up`.`project_id` = `my_table`.`project_id` AND `up`.`deleted_at` IS NULL AND `up`.`user_id` = 1
JOIN `users_projects` `up` ON `up`.`project_id` = `my_table`.`project_id` AND `up`.`deleted_at` IS NULL AND `up`.`user_id` = 1
WHERE `my_table`.`deleted_at` IS NULL
AND `my_table`.`deleted_at` IS NULL


Now I must use this hack in function findAllByUser():
PHP Code:
$compiledSelect $this->getCompiledSelect(false);
if (
strpos($compiledSelect'users_projects') === false)
{
    
$this->join('users_projects up''up.project_id = '.$this->table.'.project_id AND up.deleted_at IS NULL AND up.user_id = '.intval($user_id));

Reply




Theme © iAndrew 2016 - Forum software by © MyBB