UpdateAttributes does not work - php

I have a problem with UpdateAttributes, it seem to not work for me.
When I issue:
$ret = $sphinx->UpdateAttributes ( "products", array ("status"), array(506607786 => array(10)) );
it returns 1, but search still returns status as old value for this.
When I try
$ret = $sphinx->UpdateAttributes ( "products", array ("status", "image_id"), array(506607786 => array(10, 6666)) );
it returns 0 (false)
Does this function even work ?
Ok I have found (sphinx docs are ugly) that when issuing updateAtrributes() from PHP app then I will not see the results in search command line. However one problem still
exist - I'm not able to update 2 attributes in one updateAtrributes() - seperatly they are fine - any clues why ?

When UpdateAttributes returns 0 (not false) it does not mean it didn't work, what it means is it didn't find anything to update, basically no updates commited. A return of -1 actually means this function did not work.
Make sure that 506607786 is actually an id in your Sphinx index and that products is the name of your index.
To make the question more helpful you can provide an example row from your table, preferrably the one used in this function defined as 506607786. You cna also provide a full set of your code to make it easier.
As a side note: UpdateAttributes does not act like a realtime index. You will need to filter on these attributes specifically in your query in order for sphinx to take their new values into consideration.

Related

Search results with Sphinx and PHP by partial string

I am try to getting started with Sphinx. I add some results to index, download sphinxapi.php, and when I do this:
$cl = new SphinxClient();
$cl->SetServer( "localhost", 9312 );
// SPH_MATCH_ALL will match all words in the search term
$cl->SetMatchMode(SPH_MATCH_ALL);
$result = $cl->Query("test");
I getting this (row with id = 5 where title = test):
array (size=1)
5 => // id of post in database
array (size=2)
'weight' => string '2' (length=1)
'attrs' =>
array (size=0)
empty
But why I didnt get row from database with id = 6, where title field equal to test1 ?
And $cl->SetMatchMode(SPH_MATCH_ALL); fire error:
DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API
I comment this line in code of api file:
trigger_error ( 'DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API', E_USER_DEPRECATED );
But I dont know if it fine. Can somebody help me to understand what I am doing wrong? Thanks!
To get 'substring' matches, you need to specifically enable them.
http://sphinxsearch.com/docs/current.html#conf-min-prefix-len
(or min_infix_len)
If you dont want to see the depreciated notice, then set error_reporting
http://php.net/manual/en/function.error-reporting.php
(but even better is to rewrite the code to avoid calling the depreciated method)
warning setmatchmode
SetMatchMode are deprecated, you can still use it but it can be removed in next versions.
More info about it in:
http://sphinxsearch.com/docs/current.html#api-func-setmatchmode
http://sphinxsearch.com/blog/2013/09/11/deprecations-and-changes-in-the-2-2-series/
extracted from sphinx forum (barryhunter):
Changing the 'match mode' actually did TWO things, it changed the matching >behaviour - by
rewriting the query itself. AND changing the ranking mode.
By decoupling these concepts, I guess the idea is reduce confusion.
(for example, as soon as you choose a different matching mode, you can't >actully choose a
ranking mode)
... the match modes made sence before the 'extended syntax' was fully developed, but now
everything can be done directly via the extended syntax.
about search results
barryhunter answer is right
I suggest to read more about charset tables, morphology and stemming because i think are a better way to achieve success search than wilcard searches.
http://sphinxsearch.com/docs/current.html#conf-charset-table
http://sphinxsearch.com/docs/current.html#conf-morphology

Use a parameter as an index in PHP

I am trying to do a thing that I dont know if can work. Also accept other ideas.
I have an array passed as a parameter where the index is the task id in the database and the value is the last syncronization for the task with the database
$sync=Array
(
[22805] => 1406822699
[22806] => 1406824500
[22807] => 1406838670
)
Then I do a select in the database which gives me the whole of tasks and I one to update on the database only some tasks, basically the ones that are out of date.
//$tasks is the list of all tasks from the database and $sync is the array which is pased by the user
foreach($tasks as $task)
{
if($task['sync']<=$sync[$task['taskList_id']])
{}
else
{//to be updated
$taskModel->updateLastSync($task['taskList_id'],$time);
$task['sync']=$time;
}
}
This is the problematic line and what I need to know how to do.
$sync[$task['taskList_id']]
I want to use a parameter as index to get the value of an array.
How can I achieve this.
Because this other idea is another foreach for $sync inside the foreach for $tasks
Without seeing the full script or having access to var_dump at certain locations it's hard to say what's going on but here are some things to check:
Make sure you're getting results from the database query and that they are assigned to the $tasks variable
Make sure your $sync array values and $task['sync'] are integer types and not strings or the '<=' comparison may have unexpected results
I don't know where $time gets its value from based on code I see so verify that it does actually have a value when you try to set it
A good first step when debugging is to try to isolate the problem's location first and then worry about the cause. Using something like var_dump to verify that values of certain variables are as expected at various locations in your script is very useful. Once you know where things start going wrong you can focus your attention in the right place to find out why and come up with a solution. I recommend doing this and letting us know what you find.
I solved my question.
Which was if this is allowed, I tested and it is.
$ar=array('1'=>'a','2'=>'b','3'=>'c');
$index=2;
echo $ar[$index];

Determine if CodeIgniter active record have set a WHERE-clause

Sooo! Well, the title should say most of it.
Are there any way to check if $this->db-where('stuff', $data) has been set previously?
I could go out and make a bunch of testing of my own code (flags ect...), but I would like to know if anyone has knowledge of some fast and easy way to do it!
Thanks in advance!
[EDIT - 24-Apr-14]
It's my own code. I have to variable that can be null, String or an array. They are $categories and $budget. So if budget is set to 100$ and category is set to stuff then I will need to use db->or_where, but if only one of them is set, I need only to use db->where. In this case it is quite simple just to check if first is set, but what if more values are used? I hope you get the point :)
Yes you can easily determine whether a where clause is set or not. In CodeIgniter the Active Record class stores the wheres in an array called ar_where. You can use this array to determine if any where clauses are set or not.
For example check the following code snippet :
$this->db->select()
->from("foo")
->where('bar',NULL);
$this->db->or_where('xyz',NULL);
print_r(sizeof($this->db->ar_where));
The following will output a value of 2. So you can use this array to check for your result.

How to filter mongodb result for datatables?

I use this script to get the collection of my mongo database: http://datatables.net/development/server-side/php_mongodb
My question is: how to retrieve the rows where foo == 'mystring' only?
As you will notice (on line 29) from the source code in the file the mongo collection has been given the name: $m_collection as such:
$m_collection->find(array('foo' => 'mystring'))
Should work.
If this is not what you are looking for maybe you can be more specific and explain exactly what you are trying to do.
UPDATE
It has come to my attention you might want to instead edit the $searchTermsAll variable to search by this field in a doc. By the looks of it this PHP class links in the same as it would normally for SQL as such you should need to do anyhting special and can just enable filtering on datatables and add the value mystring to the foo field.
However to know if it is the right answer you will need to clarify.
UPDATE 2
A more destructive way of doing this that should keep filtering is to replace line 99 with:
$cursor = $m_collection->find(array_merge($searchTerms,
array('foo' => 'mystring')), $fields);
That will always make sure that your condition is added to the search terms but keeps the users own search terms.
Use as below
$cursor = $collection->find(array("foo" => "mystring"));
Here is more details: http://www.php.net/manual/en/mongo.queries.php

CakePHP find returns same value every time

Hey guys, been having this problem for a while now, and can't for the life of me seem to track down anything remotely helpful.
I'm trying to, once a user logs into the application (using the built-in Auth component), use the school_id field to find the name of the school that they are associated with (and display this name in the header of the view). I also figure that I will need to call up various other pieces of school information in other actions down the road.
I've tried both of the following, but neither seems to work. No matter whether I School->find() based on the user's "school_id" or by a number that I have included manually. It simply returns the information of the same school every time (the school with the ID of 1).
Here's what I've tried:
$this->set('school_name', $this->School->find('first', array('conditions' => array('School.id' == 2))));
$this->set('school_info', $this->School->find('first', array('conditions' => array('School.id' == $this->Auth->User('school_id')))));
$this->set('school_info', $this->School->find($this->Auth->User('school_id');
Once again, not a problem with the code not returning anything. It just returns the same school every time (where ID = 1).
As you can imagine, this has been fairly frustrating, and I would love any help that you could provide.
Thanks,
Ben
You are using the == sign instead of => in your condition.
It should be:
$this->School->find('first', array('conditions' => array('School.id' => 2))));
Remember: == is a conditional operator. => is the arrow notation used to create array key-value pairs.
A short way to do it would be:
$this->School->findById($this->Auth->user('school_id'));
If you're supplying the id, supply it to the findById and not the all-encompassing find method. That said, you should still take care to see that you're using the right operators. :)
Quick Note: 'School.id' == 2 evaluates to false and array(false) is an array with one element false which is why you didn't get any errors.

Categories