I am using codeigniter and active record. I am selecting my data over WHERE with array. Any like this. But how can I insert tags '>' and '<'? It is possible?
$whereQuery['service.service_end_date'] = $start;
Thank you for replies.
This might be what you want:
Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
You can include your own operators using this method as well:
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);
Source:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
http://ellislab.com/codeigniter/user-guide/database/active_record.html
$whereQuery['service.service_end_date >'] = $start;
$whereQuery['service.service_end_date <'] = $start;
You can pass > < <> in CI where function
$this->db->where('field_name <', "Condition_value");
From Codeigniter page :
You can include an operator in the first parameter in order to control the comparison:
$this->db->where('name !=', $name);
$this->db->where('id <', $id);
// Produces: WHERE name != 'Joe' AND id < 45
Related
I'm having an issue with my custom 'where' clause as it would also include the post attachment on the search results.
Here's the filter function
function title_filter( $where, $wp_query ){
global $wpdb;
if( $search_term = $wp_query->get( 'title_filter' ) ) :
$search_term = $wpdb->esc_like( $search_term );
$search_term = ' \'%' . $search_term . '%\'';
$title_filter_relation = ( strtoupper( $wp_query->get( 'title_filter_relation' ) ) == 'OR' ? 'OR' : 'AND' );
$where .= ' '.$title_filter_relation.' ' . $wpdb->posts . '.post_title LIKE ' . $search_term;
endif;
return $where;
}
and here are my parameters
$args = array(
'post_type' => 'bb_destinations',
'posts_per_page' => -1,
'suppress_filters' => false,
'title_filter' => $search,
'title_filter_relation' => 'OR',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'meta_key' => 'offer_repeater_%_destination_offer_price',
'orderby' => 'meta_value',
'order' => 'ASC',
);
it seems the search term would also include the attachments uploaded to the post which is weird. How do I exclude them?
for example, I would search the term 'Zia Hotel', it would also include the attachments uploaded to that post.
I am trying to update the table.
I have all values in foreach. The unique id is 'uuid'
But I would like to update if the value has been changed only. I tried to do this but no luck.
$results = DB::table('urls')
->where('uuid','=',$uuid)
->orWhere('id_media', '!=',$id_media)
->orWhere('region', '!=',$region)
->orWhere('page', '!=',$page)
->orWhere('audience', '!=',$audience)
->update(array(
'id_media' => $id_media,
'region'=>$region,
'page'=>$page,
'audience'=>$audience
));
what would be the laravel way for below query.
update my_table set
my_col = 'newValue'
where id = 'someKey'
and my_col != 'newValue';
try this.
find more in https://laravel.com/docs/5.2/queries#updates.
DB::table('my_table')
->where('id', 1)
->where('my_col', '!=', 'newValue')
->update(['my_col' => 'newValue']);
In your particular case, you should use this:
DB::table('urls')
->where('uuid', '=', $uuid)
->where(function ($query) use ($id_media, $region, $page, $audience) {
$query->orWhere('id_media', '!=', $id_media)
->orWhere('region', '!=', $region)
->orWhere('page', '!=', $page)
->orWhere('audience', '!=', $audience);
})
->update([
'id_media' => $id_media,
'region' => $region,
'page' => $page,
'audience' => $audience
]);
The last would produce something like this:
update my_table set
my_col = 'newValue'
where id = 'someId' and
(my_col1 != 'newValue1' or my_col2 != 'newValue2' or .. );
I have this form :
$builder
->add('restaurantsFilter1', 'entity', [
'label' => 'Commune',
'empty_value' => 'Dans toute la Narbonnaise',
'class' => 'AppBundle:City',
'choice_label' => 'name',
'query_builder' => function (EntityRepository $er) {
return $er
->createQueryBuilder('c')
->addSelect('d')
->leftJoin('c.documents', 'd')
->where('d.type = :type')
->orderBy('c.name')
->setParameter('type', Document::T_VILLAGE)
;
},
])
which is a select which displays a list of cities.
A client told me that he needed a field "Around me" which will display all cities around 20 km.
So to do so, I created a new city in my database with this name, but now I need to put it in the first position of my select.
In sql I would use something like ORDER BY (insee_code= '[specific_code_of_the_city]') but I dont know how I could that with the query builder.
Do you have an idea how I could do that with the symfony query builder ?
EDIT: That's the exact issue that How do I return rows with a specific value first?
You could create a hidden field and order by that.
return $er
->createQueryBuilder('c')
->addSelect('CASE
WHEN c.name = "specific_code_of_city"
THEN 0
ELSE 1
END as HIDDEN presetOrder')
->addSelect('d')
->leftJoin('c.documents', 'd')
->where('d.type = :type')
->orderBy('presetOrder', 'ASC')
->addOrderBy('c.name', 'ASC')
->setParameter('type', Document::T_VILLAGE)
;
The main idea of this post is that I need to combine search inside an array via "like" without doing a fetch.
In the following code I extract the zones (part of postal code) depending on session ID:
$sess_id = $this->Session->read('Auth.User');
$this->loadModel('Zone', 2);
$MyZones = $this->Zone->find('list', array('recursive' => -1, 'conditions' => array('Zone.user_id' => $sess_id['id']), 'fields' => array('Zone.zone')));
$this->set('MyZones', $MyZones);
print_r($MyZones);
//output : $MyZone = Array ( [1] => AB [2] => AL [3] => B1 )
Now that I have my array I want to look for persons who live in this zone (example his postalcode can be AB526PQ so he lives in the administrator zone)
$this->loadModel('Person', 2);
$num_persons = $this->Person->find('count', array('recursive' => -1, 'conditions' => array('Person.pc LIKE' => '%' . $MyZones)));
$this->set('num_persons', $num_persons);
I got the following error :
Notice (8): Array to string conversion [APP\Controller\ZonesController.php]
This is the generated sql query:
SELECT COUNT(*) AS `count` FROM `tn`.`personnes` AS `Personne` WHERE `Personne`.`pc` LIKE '%Array'
You will have to break your condition from
$conditions = array('Person.pc LIKE' => '%' . $MyZones);
to
foreach($MyZones as $MyZone) {
$conditions[] = array('Person.pc LIKE' => '%' . $MyZone);
}
I'm building a very simple module for Prestashop 1.6 and I added an admin interface that allows to list my records, a form to add and edit and delete.
This is working fine as you can see here:
The problem is that draggable reorder button is not draggable. Hence the reordering is not working...
According to the official docs, if you set the position option in your controller, you get the draggable functionality:
['position'] => 'position', // If set to position, the field will display arrows and be drag and droppable, which will update position in db (optional).
This is the relevant part of my module controller:
$this->fields_list = array(
'id_quicklinks' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25
),
'titulo' => array(
'title' => $this->l('Titulo'),
'width' => 'auto'
)
, 'lead' => array(
'title' => $this->l('Subtitulo'),
'width' => 'auto'
),
'position' => array(
'title' => $this->l('Ordem'),
'filter_key' => 'a!position',
'position' => 'position',
'align' => 'center',
'class' => 'fixed-width-md'
),
'active' => array(
'title' => $this->l('Publicado'),
'width' => '25',
'active' => 'status'
)
);
As you can see in the print screen, is shows the handles but the drag-an-drop doesn't work. No javascript errors on the console, no nothing... And I can see in the source code that jQuery and jQueryUI are loaded. Other admin pages with reorder feature are working fine...
Any ideas?
Thanks.
Well, if anyone is interested, I managed to get this to work.
In my module admin controller, I've added (before the __construct method, right after opening the Class):
protected $position_identifier = 'id_quicklinks';
id_quicklinks is the primary key of the database table this module uses.
This enabled the drag and drop feature that I was looking for, but altough I could now drag and drop, the order was not saved in the database.
For that to work I've added two more methods adapted from the controllers/admin/AdminCarriersController.php and classes/Carrier.php:
public function ajaxProcessUpdatePositions()
{
$way = (int)Tools::getValue('way');
$id_quicklinks = (int)Tools::getValue('id');
$positions = Tools::getValue('quicklinks');
if (is_array($positions))
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int)$pos[2] === $id_velcroquicklinks)
{
if (isset($position) && $this->updatePosition($way, $position, $id_quicklinks))
echo 'ok position '.(int)$position.' for id '.(int)$pos[1].'\r\n';
else
echo '{"hasError" : true, "errors" : "Can not update id '.(int)$id_quicklinks.' to position '.(int)$position.' "}';
break;
}
}
}
And:
public function updatePosition($way, $position, $id)
{
if (!$res = Db::getInstance()->executeS('
SELECT `id_quicklinks`, `position`
FROM `'._DB_PREFIX_.'quicklinks`
ORDER BY `position` ASC'
))
return false;
foreach ($res as $quicklinks)
if ((int)$quicklinks['id_quicklinks'] == (int)$id)
$moved_quicklinks = $quicklinks;
if (!isset($moved_quicklinks) || !isset($position))
return false;
var_dump($moved_quicklinks['position']);
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
return (Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_quicklinks['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_quicklinks['position'].' AND `position` >= '.(int)$position.'
'))
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position` = '.(int)$position.'
WHERE `id_quicklinks` = '.(int)$moved_quicklinks['id_quicklinks']));
}
I hope this helps someone with the same problem.
Thank you for your solution.
It took me quite a long time to realize that ajaxProcessUpdatePositions() is somehow call by itself. For those struggling to implement that method, note that echo and var_dump() won't print anything when the method is called when moving element in your table.
Also, I found someway to optimize your code a little bit.
You could replace:
if (!$res = Db::getInstance()->executeS('
SELECT `id_quicklinks`, `position`
FROM `'._DB_PREFIX_.'quicklinks`
ORDER BY `position` ASC'
))
return false;
foreach ($res as $quicklinks)
if ((int)$quicklinks['id_quicklinks'] == (int)$id)
$moved_quicklinks = $quicklinks;
By :
if (!$moved_quicklinks = Db::getInstance()->executeS('
SELECT `id_quicklinks`, `position`
FROM `'._DB_PREFIX_.'quicklinks`
WHERE `id_quicklinks` = ' . (int) $id . '
LIMIT 1;'
))
return false;
The fewer rows (and fields) returned by the database is the best. Moreover, it remove the need of a loop in your PHP code.
You could also merge both your update queries in the same database call:
return (Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_quicklinks[0]['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_quicklinks[0]['position'].' AND `position` >= '.(int)$position.'
') . '; UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position` = '.(int)$position.'
WHERE `id_quicklinks` = '.(int)$moved_quicklinks[0]['id_quicklinks']);
Database calls are costly operations, so reducing them to the minimum is a must. Like in your PHP code, the second query won't be executed if the first fails.
Here's how my ajaxProcessUpdatePositions() looks like:
public function ajaxProcessUpdatePositions()
{
$id = (int) Tools::getValue('id');
$positions = Tools::getValue("priority_level");
$way = (boolean) Tools::getValue('way');
//"and $id" because an ID of 0 is invalid (at least if you are using an auto-increment PK)
if (is_array($positions) and $id)
{
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int) $pos[2] === $id)
{
$position = (int) $position;
if ($priorityLevel = Db::getInstance()->executeS(
'SELECT `id_priority_level`,
`priority_level`
FROM `pc_job_priority`
WHERE `id_priority_level` = ' . $id . '
LIMIT 1'
)) {
Db::getInstance()->execute(
'UPDATE `pc_job_priority`
SET `priority_level` = `priority_level` '.
($way ? '- 1' : '+ 1') . '
WHERE `priority_level`' . (
$way ?
' > '. (int) $priorityLevel[0]['priority_level'] .
' AND `priority_level` <= '. $position :
' < '. (int) $priorityLevel[0]['priority_level'] .
' AND `priority_level` >= '. $position
) . ';
UPDATE `pc_job_priority`
SET `priority_level` = ' . $position . '
WHERE `id_priority_level` = ' . $id
);
}
break;
}
}
}
}