How to make multiple where->like() with Zend Framework 2? - zend-framework2

I tried making a nest on a :
$where = new Where();
I also tried making multiple :
$where->like
Could someone please provide me an example of how I can make multiple like ?
I would like to search two different fields with the same value %$value%
Thank you and best regards

Within the Where object you can NEST (wrap in parenthesis) your options and specify an operator (in this case, OR):
$where = new Where();
$where->NEST
->like('field1', '%value%')
->OR
->like('field2', '%value%')
->UNNEST;
This will generate:
... WHERE (`field1` LIKE '%value%' OR `field2` LIKE '%value%')

Found solution by using OR on the final select. The following example shows it :
$where = array();
$where[] = $rel->field1 . " LIKE '%". $value ."%' ";
$select->where($where, 'OR');
I'm not including the foreach but you get the idea, add your queries in where and use OR as predicates will generated for multiple entries.

Extending above solution with more functional way :
$where = new Where();
$where->nest()
->like('field1', '%value%')
->OR
->like('field2', '%value%')
->unnest();

Related

EPStatmentObject model not working?

Im trying to modify an epl by compiling it using the compileEPL() method and add more to e.g the where clause, however im havin trouble getting it work.
Lets say this is my epl:
select * from event where A = 1
and I want to add another where condition using the AND
and I compile the epl using compileEPL()
model.getWhereClause().getChildren().add(Expressions.and()
.add(Expressions.eq("B", )));
instead of giving me:
select * from event where A = 1 and B = 2 it just gives ..where A = 1 and not adding the new where clause.
Am I doing it wrong? The EPStatementObjectModel works fine for building an object EPL from scratch but not when compiling it and adding or modify it.
Does anyone know? Thanks.
The where-clause is rooted in an EQ since "A=1".
Expression equalsExpr = model.getWhereClause();
So construct an AND clause that holds the old EQ and the new EQ.
Expression and = Expressions.and().add(equalsExpr).add(Expressions.eq("B", ...));
Finally set the "and" as the new where-clause:
model.setWhereClause(and);
In summary, when modifying expressions to add "and": when old expression is not itself an AND you should build an AND node and add the old expression and new ones.

How to chef search for role with OR

I want to search nodes with role "mapreduce-datanode" & "mapreduce-namenode".
So i tried following :
hadoop_nodes = search(:node, "role:mapreduce-datanode OR role:mapreduce-namenode AND chef_environment:#{node.chef_environment} AND domain:#{node['domain']}")
Is this correct way to do ?
Thanks.
If you want "mapreduce-datanode" & "mapreduce-namenode", as you state, then why are you using an OR?
If you really want an AND, then you want:
hadoop_nodes = search(:node, "role:mapreduce-datanode AND role:mapreduce-namenode AND chef_environment:#{node.chef_environment} AND domain:#{node['domain']}")
If you want the union of the two sets (ie you really did mean OR) then try this:
hadoop_nodes = search(:node, "(role:mapreduce-datanode OR role:mapreduce-namenode) AND chef_environment:#{node.chef_environment} AND domain:#{node['domain']}")

zf2 How to Select Where Like Upper case

I trying to get some title from my database. But it doesn`t works:
$select->where->like('LOWER(title)','%'.$search_by.'%');
without LOWER it works fine.
$select->where->like('title','%'.$search_by.'%');
but I need LOWER case.
like() only seems to accept an identifier as its first parameter.
You should try this
$select->where->literal('LOWER(title) LIKE "%'.$search_by.'%"');
// or
$select->where->expression('LOWER(title) LIKE ?', '%'.$search_by.'%');
I have found a solution (in ZF3) for people who searched for:
use Zend\Db\Sql\Expression;
$upperExpr = new Expression('LOWER(?)', [title], [Expression::TYPE_IDENTIFIER]);
$select->where->like($upperExpr, "%" . mb_strtolower($search_by, 'UTF-8') . "%");
Or only strtolower() if you have no 'äÄöÖüÜ' ;)
This will create the right SQL-Statement.

autocomplete, search multiple database tables/fields - Symfony

I have 3 models:
article [name, title]
photo [name, description]
video [name, description, transcription]
Now, I want to create an autocomplete search that will search each of those models and db fields for matching string in the input field.
Is it possible to do a search over these 3 tables?
If so, how would I go about it?
UNION operator is what you're looking for.
I guess your using propel.
Its absolutetly possible to do this, but if these table are not related to anything that could result in some messy code when saving. But if you absolutely need to search in those tables, my approach would be to create an action like:
public function executeAutocomplete(sfWebRequest $request)
{
$q = $request->getParameter('q');//The value to be searched
$limit = $request->getParameter('limit');//Not completly necessary but recommendable
if(count($q) == 0)
{
return sfView::NONE;//With no input, no search
}
$results = array( 'article' => array(), 'photo' => array(), 'video' => array());
$article_search = ArticlePeer::search($q,$limit);
$photo_search = PhotoPeer::search($q,$limit);
$video_search = VideoPeer::search($q,$limit);
$this->process_search($results,$article_search,$photo_search,$video_search);
//Process those three arrays to fit your need
return $this->renderText(json_encode($result));
}
Now, the search function on the Peer classes could look like this:
ArticlePeer>>
public static function search($q,$limit = null)
{
$c = new Criteria();
$c->add(self::NAME,'%'.$q.'%',Criteria::LIKE);
if(!is_null($limit))
{
$c->addLimit($limit);
}
return self::doSelect($c);
}
Finally as for the widget, i have used and adapted sfWidgetJQueryAutocomplete and it work pretty good so you should check it out.
EDIT: The short way to an embbed search field si creating sfForm wit the jQuery widget i mentioned before and leave configuration and js to the widget. You'll have to find a way to handle search resutls.
Well i hope this helped you!

Select attribute from node with XPath

I have a file with the following structure
<admin>
<sampleName>Willow oak leaf</sampleName>
<sampleDescription comment="Total genes">
<cvParam cvLabel="Bob" accession="123" name="Oak" />
</sampleDescription>
</admin>
I'm trying to get out the text "Total genes" after the sampleDescription comment, and I have used the following code:
sampleDescription = doc.xpath( "/admin/Description/#comment" )
sampleDescription = doc.xpath( "/admin/Description" ).text
But neither work. What am I missing?
might be a typo... have you tried doc.xpath("/admin/sampleDescription/#comment").text?
It's not working because there's no Description element. As mentioned by Iwe, you need to do something like sampleDescription = doc.xpath("/admin/sampleDescription/#comment").to_s
Also, if it were me, I would just do sampleDescription = doc.xpath("//sampleDescription/#comment").to_s. It's a simpler xpath, but it might be slower.
And as a note, something that trips up a lot of people are namespaces. If your xml document uses namespaces, do sampleDescription = doc.xpath("/xmlns:admin/sampleDescription/#comment").to_s. If your doc uses namespaces and you don't specify it with xmlns:, then Nokogiri won't return anything.
Try this:
doc.xpath("//admin/sampleDescription/#comment").to_s
doc.xpath returns a NodeSet which acts a bit like an array. So you need to grab the first element
doc.xpath("//admin/sampleDescription").first['comment']
You could also use at_xpath which is equivalent to xpath(foo).first
doc.at_xpath("//admin/sampleDescription")['comment']
Another thing to note is that attributes on nodes are accessed like hash elements--with [<key>]

Resources