Problem in Model

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
Slushgood
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Tue Sep 22, 2015 1:04 pm

Problem in Model

Post by Slushgood » Thu Jun 17, 2021 3:48 pm

Hello
I am faced with a dilemma.
To perform an advanced search model more precise than the one proposed by Joomla I created my own model.

Code: Select all

  
    class Book extends DataModel
{
    
    /**
     * @return $this
     *
     * @throws RecordNotLoaded
     */
    public function hit()
    {
        if(!$this->getId())
        {
            throw new RecordNotLoaded("Can't change the state of a not loaded DataModel");
        }
        
        if (!$this->hasField('hits'))
        {
            return $this;
        }
        
        $enabled = $this->getFieldAlias('hits');
        
        $this->$enabled = $this->$enabled + 1;
        $this->save();
        
        return $this;
    }
    
    /**
     * @param \JDatabaseQuery  $query
     * @param boolean          $overrideLimits
     *
     * @return void
     */
    public function onAfterBuildQuery($query, $overrideLimits = false)
    {
        /**
         * TMP FIX
         */
        $db = $this->getDbo();
       
        $query->clear('order');      
        $order = $this->input->get('filter_order');
        $order = $db->qn($order);

        $dir = strtoupper($this->getState('filter_order_Dir', null, 'cmd'));
        
        if (!in_array($dir, ['ASC', 'DESC']))
        {
            $dir = 'ASC';
            $this->setState('filter_order_Dir', $dir);
        }
        //$query->group('`book`.`nglibrary_book_id`');

        $query->order($order . ' ' . $dir);
        
        /**
         * /TMP FIX
         */
    }
    
    /**
     * @param \JDatabaseQuery  $query
     * @param boolean          $overrideLimits
     *
     * @return void
     */
    
    public function onBeforeBuildQuery($query,$overrideLimits = false)
    {
        /**
         * TMP FIX
         */
       $query->clear('from')->from($this->getTableName() . ' AS book');
        $this->setBehaviorParam('tableAlias', 'book');
        $query->clear('group');
        $outerGlue = $this->input->get('_outerGlue');
        $query->group('book.nglibrary_book_id');
        $query->clear('select');
        //$query->from(array('#__nglibrary_books AS book','#__nglibrary_copies AS copie','#__nglibrary_books_authors AS link_author','#__nglibrary_authors AS author ','#__nglibrary_editors AS editor'));
        $query->select('book.*,copie.*,author.*');


        //Recherche dans la table __Copies

            //Join _Copies
            $query->leftjoin('#__nglibrary_copies AS copie ON book.nglibrary_book_id = copie.nglibrary_book_id');
            
            //Join _Authors
            $query->leftjoin('#__nglibrary_books_authors AS link_author ON book.nglibrary_book_id = link_author.nglibrary_book_id');
            $query->leftjoin('#__nglibrary_authors AS author ON author.nglibrary_author_id = link_author.nglibrary_author_id');
            
            //Join _Editors
            $query->leftjoin('#__nglibrary_editors AS editor ON book.nglibrary_editor_id = editor.nglibrary_editor_id');
            
        if ($this->input->get('_resetState') == 1)
        {
            // Save the state we need to keep
            $limit = $this->getState('limit');
            
            // Reset model and user state
            $this->clearState()->reset();
            \JFactory::getApplication()->setUserState(substr($this->getHash(), 0, -1), null);
            
            // Set back the saved state
            $this->input->set('limit', $limit);
            $this->setState('limit', $limit);
        }
        //Process pour la recherche via le module
        if($this->input->get('search') !=''){
            $search = $this->input->get('search');
            
            $books = $this->getState('books');
            $copies = $this->getState('copies');
            if($this->input->get('title') =='Y')
            {   $this->input->set('title',NULL);
                //$this->input->set('title',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['title' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_title', 'OR'); 
            }
            if($this->input->get('author_adv') =='Y')
            {   $this->input->set('author_adv',NULL);
                //$this->input->set('author_adv',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['author_adv' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_author', 'OR');                
            }
            if($this->input->get('serie') =='Y')
            {   $this->input->set('serie',NULL);
                //$this->input->set('serie',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['serie' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );               
                $this->input->set('operateur_serie', 'OR');
            }
            if($this->input->get('collection') =='Y')
            {   $this->input->set('collection',NULL);
                //$this->input->set('collection',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['collection' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_collection', 'OR');                   
            }
            if($this->input->get('resume') =='Y')
            {   $this->input->set('resume',NULL);
                //$this->input->set('resume',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['resume' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_resume', 'OR');
            }
            if($this->input->get('isbn') =='Y')
            {   $this->input->set('isbn',NULL);
                //$this->input->set('isbn',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['isbn' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_isbn', 'OR');
            }
            if($this->input->get('editor') =='Y')
            {   $this->input->set('editor',NULL);
                //$this->input->set('editor',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['editor' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_editor', 'OR');
            }
            if($this->input->get('interest') =='Y')
            {   $this->input->set('interest',NULL);
                //$this->input->set('interest',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['interest' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_interest', 'OR');
            }
            if($this->input->get('support') =='Y')
            {   $this->input->set('support',NULL);
                //$this->input->set('support',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['support' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_support', 'OR');
            }
            if($this->input->get('publics') =='Y')
            {   $this->input->set('grp_public',NULL);
                //$this->input->set('public',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['grp_public' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_grppublic', 'OR');
            }
            if($this->input->get('number') =='Y')
            {   $this->input->set('number',NULL);
                //$this->input->set('support',$this->input->getString('search'));
                $this->input->set('copies',
                    array_merge(['number' => $this->input->getString('search')],$this->input->get('copies', [], 'array') )
                    );
                $this->input->set('operateur_number', 'OR');
            }
            if($this->input->get('grp_genre') =='Y')
            {   $this->input->set('grp_genre',NULL);
            //$this->input->set('support',$this->input->getString('search'));
            $this->input->set('books',
                array_merge(['grp_genre' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                );
            $this->input->set('operateur_genre', 'OR');
            }
            if($this->input->get('genre') =='Y')
            {   $this->input->set('genre',NULL);
                //$this->input->set('support',$this->input->getString('search'));
                $this->input->set('books',
                    array_merge(['genre' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
                    );
                $this->input->set('operateur_genre', 'OR');
            }
                //OPTIONNAL
                if($this->input->get('origin') =='Y')
                {   $this->input->set('origin',NULL);
                    //$this->input->set('support',$this->input->getString('search'));
                    $this->input->set('copies',
                        array_merge(['origin' => $this->input->getString('search')],$this->input->get('copies', [], 'array') )
                        );
                    $this->input->set('operateur_support', 'AND');
                }
                if($this->input->get('library') =='Y')
                {   $this->input->set('library',NULL);
                    //$this->input->set('support',$this->input->getString('search'));
                    $this->input->set('copies',
                        array_merge(['library' => $this->input->getString('search')],$this->input->get('copies', [], 'array') )
                        );
                    $this->input->set('operateur_library', 'AND');
                }
        }
             

        //Process pour la recherche avancé
        if($this->input->get('button_menu') !=''||$this->input->get('advancedsearch') !=''|| $this->input->get('search') !=''){
            if ($this->input->get('copiesStatusId'))
            {
                $this->input->set('copies',
                    array_merge(['statusId' => $this->input->get('copiesStatusId')],$this->input->get('copies', [], 'array') )
                    );
            }
            if ($this->input->get('copiesLibraryId'))
            {
                $this->input->set('copies',
                    array_merge(['libraryId' => $this->input->get('copiesLibraryId')],$this->input->get('copies', [], 'array') )
                    );
            }
            if ($this->input->get('origin'))
            {
                $this->input->set('copies',
                    array_merge(['origin' => $this->input->getString('origin')],$this->input->get('copies', [], 'array') )
                    );
            }
            
            if ($this->input->get('location'))
            {
                $this->input->set('copies',
                    array_merge(['location' => $this->input->getString('location')],$this->input->get('copies', [], 'array') )
                    );
            }
            
            
            if (($copies = $this->getState('copies'))
                && array_filter($copies))
                {	$sql = '';
                    if (!empty($copies['number']))
                        $sql .= ('' . $this->input->get('operateur_exemplaire').'(`copie`.`number` = "' . $copies['number'] . '")');
                        
                    if (!empty($copies['cote']))
                        $sql .= ('' . $this->input->get('operateur_cote').'(`copie`.`cote` LIKE "' . $copies['cote'] . '%")');
                            
                    if (!empty($copies['origin']))
                         $sql .= ('' . $this->input->get('operateur_origin').'(`copie`.`origin` LIKE "' . $copies['origin'] . '")');
                                
                    if (!empty($copies['location']))
                        $sql .= ('' . $this->input->get('operateur_location').'(`copie`.`location` LIKE "' . $copies['location'] . '%")');
                                    
                    if (!empty($copies['statusId']))
                        $sql .= ('' . $this->input->get('operateur_bookstatus_id').'(`copie`.`nglibrary_bookstatus_id` = "' . $copies['statusId'] . '")');
                                        
                    if (!empty($copies['libraryId']))
                        $sql .= ('' . $this->input->get('operateur_library_id').'(`copie`.`nglibrary_library_id` = "' . $copies['libraryId'] . '")');
                                                
                    if (!empty($copies['days']))
                    {$dateJ = strftime('%Y-%m-%d');
                        $year = substr($dateJ, 0, -6);
                        $month = substr($dateJ, -5, -3);
                        $day = substr($dateJ, -2);
                        
                        // récupère la date du jour
                        $date_string = mktime(0,0,0,$month,$day,$year);
                        // Supprime les jours
                        $timestamp = $date_string - ($copies['days'] * 86400);
                        $nouvelle_date = date("Y-m-d", $timestamp);

                        $sql .= ('' . $this->input->get('operateur_newunder').'(`copie`.`created_on` >= \''. $nouvelle_date .'\')');
                    }
                                                    if(substr($sql,-strlen($sql),2) == 'OR'){
                                                        $sql = substr($sql,2);
                                                    }else if(substr($sql,-strlen($sql),3) == 'AND'){
                                                        $sql = substr($sql,3);
                                                    }
                }
           
            //Recherche dans la table __Books
           
// est-ce encore utile ?
//                  if ($this->input->get('title'))
//                 {
//                     $this->input->set('books',
//                         array_merge(['title' => $this->input->getString('title')],$this->input->get('books', [], 'array') )
//                         );
//                 }
                
//                 if ($this->input->get('author_adv'))
//                 {
//                     $this->input->set('books',
//                         array_merge(['author_adv' => $this->input->getString('author_adv')],$this->input->get('books', [], 'array') )
//                         );
//                 }
//                 if ($this->input->get('serie'))
//                 {
//                     $this->input->set('books',
//                         array_merge(['serie' => $this->input->getString('serie')],$this->input->get('books', [], 'array') )
//                         );
//                 }
            
            if (($books = $this->getState('books'))
                && array_filter($books))
            {   $sql2 ='';
                if (!empty($books['title']))
                        //j'enleve les prefixes du titre pour la recherche et je cherche dans les sous-titres
                { $title = preg_replace ("#^(le |la |les |LE |LA |LES  )#","",$books['title']);        
                    $sql2 .= ('((`book`.`title` LIKE "%' . $title . '%") OR ');
                    $sql2 .= ('(`book`.`subtitle` LIKE "%' . $title . '%") OR ');
                    $sql2 .= ('(`book`.`coll5` LIKE "%' . $title . '%"))');
                }
                 if (!empty($books['resume']))
                     $sql2 .= ('' . $this->input->get('operateur_resume').'(`book`.`resume` LIKE "%' . $books['resume'] . '%")');
                
                 if (!empty($books['isbn']))
                     $sql2 .= ('' . $this->input->get('operateur_isbn').'(`book`.`isbn` = "' . $books['isbn'] . '")');
 
                 if (!empty($books['author_adv']))
                 { $sql2 .= ('' . $this->input->get('operateur_author').'((`book`.`author_p` LIKE "%' . $books['author_adv'] . '%")');
                 //Je cherche aussi dans la table authors
                 $sql2 .= ('  OR (`author`.`title` LIKE "%' . $books['author_adv'] . '%"))');
                 }
                 if (!empty($books['coll7']))
                     $sql2 .= ('' . $this->input->get('operateur_interest').'(`book`.`coll7` = "' . $books['coll7'] . '")');
                     
                 if (!empty($books['coll6']))
                     $sql2 .= ('' . $this->input->get('operateur_topic').'(`book`.`coll6` = "' . $books['coll6'] . '")');

                 if (!empty($books['editor']))
                     $sql2 .= ('' . $this->input->get('operateur_editor').'(`editor`.`title` LIKE "%' . $books['editor'] . '%")');
                     
                 if (!empty($books['collection']))
                     $sql2 .= ('' . $this->input->get('operateur_collection').'(`book`.`collection` LIKE "%' . $books['collection'] . '%")');
                     
                 if (!empty($books['serie']))
                     $sql2 .= ('' . $this->input->get('operateur_serie').'(`book`.`serie` LIKE "%' . $books['serie'] . '%")');
                 
                 if (!empty($books['year-from']))                   
                     $sql2 .= ('' . $this->input->get('operateur_inputyear').'(`book`.`year` BETWEEN '.$books['year-from'].' AND '.$books['year-to'].')');
                                              
                 if (!empty($books['support']))
                     $sql2 .= ('' . $this->input->get('operateur_support').'(`book`.`support` = "' . $books['support'] . '")');
                         
                 if (!empty($books['grp_public']))
                     $sql2 .= ('' . $this->input->get('operateur_grppublic').'(`book`.`grp_public` = "' . $books['grp_public'] . '")');
                     
                 if (!empty($books['public']))
                     $sql2 .= ('' . $this->input->get('operateur_section').'(`book`.`public` = "' . $books['public'] . '")');
                    
                 if (!empty($books['genre']))
                     $sql2 .= ('' . $this->input->get('operateur_genre').'(`book`.`genre` = "' . $books['genre'] . '")');
                     
                 if (!empty($books['statusBib']))
                     $sql2 .= ('' . $this->input->get('operateur_statutbib').'(`book`.`status_bib` = "' . $books['statusBib'] . '")');
                    
                 if (!empty($books['hideperiodique']) == 1)
                 {$sql2 .= ('AND ((`book`.`periodique` = 1 ) OR (`book`.`periodique` IS NULL ))');
                 }
                         
            }
            if(isset($sql) == false && $sql2 !=''){
                $rsql =$sql2;
            }else if(isset($sql2) == false && $sql !=''){
                //$sql2 = '(`book`.`nglibrary_book_id` IS NOT NULL)';
                $rsql=$sql;
            }else{
                $rsql= $sql2.' AND '.$sql;
            }

            if(substr($rsql,-strlen($rsql),2) == 'OR'){
                $rsql = substr($rsql,2);
            }else if(substr($rsql,-strlen($rsql),3) == 'AND'){
                $rsql = substr($rsql,3);
            }
            $query->where($rsql);
            
        return $query;
        } 
  }
}
        
Here is some of the code used.
This one works fine for all that is research.
The problem is that on my results window the filters displayed by Joomla for sorting, ordering and number of rows re-initializes my search result systematically to display everything.
Can anyone tell me how this is done ...
Thank you for your help

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 28083
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Problem in Model

Post by Per Yngve Berg » Thu Jun 17, 2021 4:06 pm

Mod.Note: Relocating the topic to the Coding forum.

Are you writing a search plugin?

https://docs.joomla.org/J3.x:Creating_a_search_plugin

Slushgood
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Tue Sep 22, 2015 1:04 pm

Re: Problem in Model

Post by Slushgood » Fri Jun 18, 2021 6:58 am

Hello Per Yngve Berg
No it is not really a plugin. It is a Model with results in View
<div class="items">
<!-- TODO peut on le faire passer par render et gerer les colonnes -->
<?php echo $this->getRenderedForm();?>
</div>
<div class="Results"></div>
</div>

and parameters in Views XML
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="com_nglibrary_books_view_listing_title" option="com_nglibrary_books_view_listing_option">
<message>
<![CDATA[COM_NGLIBRARY_BOOKS_VIEW_LISTING_DESC]]>
</message>
</layout>
<fields name="request">
<fieldset name="request">
<field
type="hidden"
readonly="readonly"
name="button_menu"
default="buttonmenu"
required="false"
>
</field>
<field
name="copiesStatusId"
type="sql"
query="SELECT nglibrary_bookstatus_id, title FROM #__nglibrary_bookstatuses WHERE is_displayed != 0"
label="COM_NGLIBRARY_STATUT"
...

Slushgood
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Tue Sep 22, 2015 1:04 pm

Re: Problem in Model

Post by Slushgood » Fri Jun 18, 2021 7:01 am

This is my original Model. This one is working good but the problem is that i can't insert OR AND parameters into the query.
<?php
/**
* @package nG Library
*
* @copyright Copyright (c) 2014-2016 Newebtime. All Rights Reserved
* @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
*/

namespace Nglibrary\Site\Model;

use FOF30\Model\DataModel;
use FOF30\Model\DataModel\Exception\RecordNotLoaded;

class Book extends DataModel
{

/**
* @return $this
*
* @throws RecordNotLoaded
*/
public function hit()
{
if(!$this->getId())
{
throw new RecordNotLoaded("Can't change the state of a not loaded DataModel");
}

if (!$this->hasField('hits'))
{
return $this;
}

$enabled = $this->getFieldAlias('hits');

$this->$enabled = $this->$enabled + 1;
$this->save();

return $this;
}

/**
* @param \JDatabaseQuery $query
* @param boolean $overrideLimits
*
* @return void
*/
public function onAfterBuildQuery($query, $overrideLimits = false)
{
/**
* TMP FIX
*/
$db = $this->getDbo();
$order = $this->getState('filter_order', null, 'cmd');

if (!array_key_exists($order, $this->knownFields))
{
$order = $this->getIdFieldName();
$this->setState('filter_order', $order);
}

$order = $db->qn($order);

$dir = strtoupper($this->getState('filter_order_Dir', null, 'cmd'));

if (!in_array($dir, ['ASC', 'DESC']))
{
$dir = 'ASC';
$this->setState('filter_order_Dir', $dir);
}

$query->order($order . ' ' . $dir);


//$query->clear('order')->order('book.' . $order);
/**
* /TMP FIX
*/
}

/**
* @param \JDatabaseQuery $query
* @param boolean $overrideLimits
*
* @return void
*/

public function onBeforeBuildQuery($query, $overrideLimits = false)
{
/**
* TMP FIX
*/
$query->clear('from')->from($this->getTableName() . ' AS book, #__nglibrary_copies AS copie ');
$this->setBehaviorParam('tableAlias', 'book');
$outerGlue = $this->input->get('_outerGlue');
//$outerGlue = echo $outerGlue ;

/**
* /TMP FIX
*/

if ($this->input->get('_resetState') == 1)
{
// Save the state we need to keep
$limit = $this->getState('limit');

// Reset model and user state
$this->clearState()->reset();
\JFactory::getApplication()->setUserState(substr($this->getHash(), 0, -1), null);

// Set back the saved state
$this->input->set('limit', $limit);
$this->setState('limit', $limit);
}

/**
* Convert simple search to normal one
*/
if ($search = $this->input->get('search', null, 'string'))
{

if ($this->input->get('title') == 'Y')
{
//j'enleve les prefixes du titre pour la recherche
$search = preg_replace ("#^(le |la |les |LE |LA |LES )#","",$search);
$this->input->set('title', $search);
$this->input->set('subtitle', $search); //OR
$this->input->set('coll5', $search); //OR autres titres
}
if ($this->input->get('isbn') == 'Y')
{
$this->input->set('isbn', $search);
}

if ($this->input->get('serie') == 'Y')
{
$this->input->set('serie', $search);
}


if ($this->input->get('collection') == 'Y')
{
$this->input->set('collection', $search);
}

if ($this->input->get('interest') == 'Y')
{
$this->input->set('coll7', $search);
}

if ($this->input->get('topic') == 'Y')
{
$this->input->set('coll6', $search);
}

if ($this->input->get('resume') == 'Y')
{
$this->input->set('resume', $search);
}
if ($this->input->get('cote') == 'Y')
{
$this->input->set('copies', ['cote' => $search]);
}
if ($this->input->get('editor') == 'Y')
{
$this->input->set('editor', ['title' => $search]);
}
if ($this->input->get('origin') == 'Y')
{
$this->input->set('copies',['origin' => $search]);
}
if ($this->input->get('author') == 'Y')
{
//Il faut creer une table avec chaque terme et effectuer une recherche de ceux ci
$search = preg_split("/[\s,]+/",$search);
$this->input->set('authors', ['title' => $search]);
}
if ($this->input->get('exemplaire') == 'Y')
{
$this->input->set('copies', ['number' => $search]);
}
}
//Pour la recherche avancé
//Recheche avancée titre et autre ...


/**
* Manage createOn using 'days'
*/
if ($this->input->get('days') >= 1)
{
$today = date('Y-m-d');
$days = date('Y-m-d', strtotime($today. ' -'. $this->input->get('days') .' days'));

$created_on['method'] = 'between';
$created_on['from'] = $days;
$created_on['to'] = $today;

$this->input->set('created_on', $created_on);
}
/**
* Manage menu configuration (array in menu are broken)
*/
if ($this->input->get('copiesStatusId'))
{
$this->input->set('copies',
array_merge(
['statusId' => $this->input->get('copiesStatusId')],
$this->input->get('copies', [], 'array')
)
);
}
if ($this->input->get('copiesLibraryId'))
{
$this->input->set('copies',
array_merge(
['libraryId' => $this->input->get('copiesLibraryId')],
$this->input->get('copies', [], 'array')
)
);
}
if ($this->input->get('origin'))
{
$this->input->set('copies',
array_merge(['origin' => $this->input->getString('origin')],$this->input->get('copies', [], 'array') )
);
}

if ($this->input->get('location'))
{
$this->input->set('copies',
array_merge(['location' => $this->input->getString('location')],$this->input->get('copies', [], 'array') )
);
}

if ($this->input->get('statusBib'))
{
$this->input->set('copies',
array_merge(['statusBib' => $this->input->getString('statusBib')],$this->input->get('copies', [], 'array') )
);
}

/**
* Handle relations filter
* - Level 1 relations, using FOF
* - Level 2 relations, using Joomla
*/
if (($editor = $this->getState('editor'))
&& array_filter($editor))
{
$this->whereHas('editor', function(\JDatabaseQuery $q) use ($editor) {
$q->where('`title` = "' . $editor['title'] . '"');
});
}

if ($author_adv = $this->getState('author_adv'))
{
//Il faut creer une table avec chaque terme et effectuer une recherche de ceux ci
$authors = preg_split("/[\s,]+/",$author_adv);
$this->input->set('authors', ['title' =>$authors]);
}

if (($authors = $this->getState('authors'))
&& array_filter($authors))
{
$this->whereHas('authors', function(\JDatabaseQuery $q) use ($authors)
{
if (!empty($authors['title']))
foreach ($authors['title'] as $searchauth)
$q->extendwhere('AND','`title` LIKE "%' . $searchauth . '%"','OR');

if (!empty($authors['id']))
$q->extendwhere('AND','`pivotTable`.`nglibrary_author_id` = ' . $q->q($authors['id']) . '','OR');
});
}

//exclure les périodques;
if ($this->input->get('hideperiodique') == 1)
{
$this->input->set('copies',
array_merge(['hideperiodique' => $this->input->getString('hideperiodique')],$this->input->get('copies', [], 'array') )
);
}

if (($copies = $this->getState('copies'))
&& array_filter($copies))
{
$this->whereHas('copies', function(\JDatabaseQuery $q) use ($copies)
{

if (!empty($copies['number']))
$q->where('`number` = "' . $copies['number'] . '"');

if (!empty($copies['cote']))
$q->where('`cote` LIKE "' . $copies['cote'] . '%"');

if (!empty($copies['origin']))
$q->where('`origin` LIKE "' . $copies['origin'] . '%"');

if (!empty($copies['location']))
$q->where('`location` LIKE "' . $copies['location'] . '%"');

if (!empty($copies['statusId']))
$q->where('`nglibrary_bookstatus_id` = "' . $copies['statusId'] . '"');

if (!empty($copies['libraryId']))
$q->where('`nglibrary_library_id` = "' . $copies['libraryId'] . '"');

if (!empty($copies['statusBib']))
$q->where('`status_bib` = "' . $copies['statusBib'] . '"');

if (!empty($copies['hideperiodique']))
$q->where('`periodique` != "' . $copies['hideperiodique'] . '"');
});
}

//\JFactory::getApplication()->enqueueMessage($query->__toString());
}
}


Slushgood
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Tue Sep 22, 2015 1:04 pm

Re: Problem in Model OR Query ?

Post by Slushgood » Mon Jun 21, 2021 3:54 pm

Is it problem in datamodel or did i switch something in my query ?
Thank you for your help


Post Reply

Return to “Joomla! 3.x Coding”