Sphinx and Primary Key Column - primary-key

I am trying to learn about the Sphinx search server. I have it working through the command line and through PHP. The only nagging thing is that when I index a table, Sphinx returns a notice that says it could not find the Primary Key column and 'ignores' that column. It works anyway, so I did not pay that much mind, but now I've noticed that I am getting PHP notices from my search script saying that the primary key column q_id is undefined. The script works and returns the proper q_ids but I do not like getting the notices.
Here is a snippet so you can see where I am trying to get the primary key column.
$row_ids = array();
if ( ! empty($result["matches"]) ) {
foreach ( $result["matches"] as $doc => $docinfo ) {
array_push($row_ids, $docinfo['q_id']);
}
I understand that Sphinx does not recognize what a Primary Key is, but I figured it could still index the column, and it must do something with it because searches are returning the correct q_ids. Where am I wrong? Thanks.

Is "q_id" an attribute?
Would be something like
foreach ( $result["matches"] as $doc => $docinfo ) {
array_push($row_ids, $docinfo['attrs']['q_id']);
}
You should do a print_r($result); to see everything returned...

Related

How can I get Rails 5 to play nicely with a primary key that has a period in it?

I'm working with a database I have no control over, and cannot make alterations to. This database has a table called warehouse_items. Each warehouse item is uniquely identified by a primary key indicating the item id.
Unfortunately, that primary key attribute is named WAREHOUSE_ITEM.ID
(Note the obnoxious period between "item" and "id")
When I try to run a basic query, such as:
WarehouseItem.find('wh3453')
I get an Undefined Table error.
Fortunately, when looking at what Rails is attempting to do, the problem becomes obvious:
: SELECT "warehouse_items".* FROM "warehouse_items" WHERE "WAREHOUSE_ITEM"."ID" = $1 LIMIT $2
Because of the period in the attribute name, Rails is treating "WAREHOUSE_ITEM.ID" as a table/attribute combination, rather than an attribute name with a period in it.
When I run the following PSQL query by hand, I get exactly what I need:
SELECT "warehouse_items".* FROM "warehouse_items" WHERE "warehouse_items"."WAREHOUSE_ITEM.ID" = 'wh3453'
Why is Rails screwing this up, and how can I fix it?
EDIT:
Also worth noting: I've tried using self.primary_key to override the primary key to no avail.
I've tried both a string and a symbol, as in:
self.primary_key="WAREHOUSE_ITEM.ID"
and
self.primary_key=:"WAREHOUSE_ITEM.ID"
Neither one has worked...
Thanks for all the help, everyone!
A suggestion in the comments to use find_by_sql does work! However, I stumbled onto a different solution that works even better.
First, I aliased the annoying attribute name to something simple: id
alias_attribute :id, :"WAREHOUSE_ITEM.ID"
Notice that it's still a symbol, which is important for the next step.
I then overwrite the primary_key method with a custom function:
def self.primary_key
return "id"
end
Now, when I do WarehouseItem.find('wh3453'), Rails defaults to checking id, which is aliased to the correct symbol and it works as intended!!!

What is the ideal way to select rows by projected keys in Groundhog Haskell

I'm trying to create a many to many table using Groundhog in Haskell, which basically looks like this if I cut away all the other logic I have:
data FooRow = FooRow {
fooRowUUID :: UUID
}
deriving instance Show FooRow
data BarRow = BarRow {
barRowUUID :: UUID
}
deriving instance Show BarRow
data FooToBarRow = FooToBarRow {
fooToBarRowUUID :: UUID,
fooToBarRowFoo :: DefaultKey FooRow,
fooToBarRowBar :: DefaultKey BarRow
}
deriving instance Show FooToBarRow
Now, trying to define operations, I can get and insert all of these records just fine, however I'm not sure how to go from having a FooRow, with it's ID, and then get all the related BarRows by way of the many to many table. Right now I've played with something like this:
getBarsForFoo fooID = do
barKeys <- project
(FooToBarRowBarField)
(FooToBarRowFooField ==. (Foo_FooKey fooID))
select $ (BarRowUUIDField `in_` barKeys)
However this doesn't typecheck, with the error:
Couldn't match type 'UUID' with 'BarRow'
Inspecting just the results of the project with putStrLn, I can see that the type of barKeys is:
[Bar_BarKey UUID]
but I don't quite understand how to make use of that within my query. I don't see any examples like this in the Groundhog documentation, so I'm hoping someone will be able to put me on the right path here.
I'm quite certain there are more efficient ways to go about this (there's going to be a bunch of underlying queries with this approach), but this does at at least get the job done for now.
getBarsForFoo fooID = do
barKeys <- project
(FooToBarRowBarField)
(FooToBarRowFooField ==. (Foo_FooKey fooID))
q <- mapM (getBy) barKeys
return (catMaybes q :: [BarRow])

Yii - CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5375' for key 'PRIMARY'

I am working with Yii 1.1.x and get the following error when performing an insert.
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:
Integrity constraint violation: 1062 Duplicate entry '5375' for key 'PRIMARY'
This happens on a table called 'competition_prizes' - there is only one primary key called 'id' (there are no other indexes or anything like that).
I can see that there is one row with the id of 5375 so that entry does already exist (as per the insert query).
The controller code is as follows has some functionality within the afterSave() functionality.
protected function setPrizes($prizes, $prize_type)
{
if(!empty($prizes) && is_array($prizes))
{
$prize_model = CompetitionPrizes::model();
$competition_id = $this->competition_id;
$prize_model->deleteAll('competition_id =:competition_id AND prize_type = :prize_type ',array(
':competition_id'=> $competition_id,
':prize_type'=> $prize_type
));
foreach($prizes as $prize_position => $prize_desc) :
$prize_model->setIsNewRecord(true);
$prize_model->setAttributes(compact('competition_id', 'prize_position', 'prize_type','prize_desc'));
$prize_model->save();
endforeach;
}
}
Any ideas of how to get around the error - please note I'm new to Yii so be gentle :)
On top of the $prize_model->setIsNewRecord(true) (or $prize_model->isNewRecord = true) you also have to empty out the id attribute. As I've mentioned in my comment above, the setIsNewRecord only determines if ether the insert or update scenario is used when you call save(). If your PK is still set it will simply attempt an insert with those values set, resulting in a duplicate error.
The following should do the trick:
$id = NULL;
foreach($prizes as $prize_position => $prize_desc) :
$prize_model->setIsNewRecord(true);
$prize_model->setAttributes(compact('id', 'competition_id', 'prize_position', 'prize_type','prize_desc'));
$prize_model->save();
endforeach;
Instead of setting new record flag,
$prize_model->setIsNewRecord(true); // sometimes it may not work.
The issues with setIsNewRecord is discussed here
You can do like this also:
$prize_model->isNewRecord = true;
Yii forum suggest to create new object inside for loop. You can check here
You have to unset values manually like $model->id = null;
You can get details here
foreach($prizes as $prize_position => $prize_desc) :
$prize_model->setIsNewRecord(true);
$prize_model->id = null;
$prize_model->setAttributes(compact('competition_id', 'prize_position', 'prize_type','prize_desc'));
$prize_model->save();
endforeach;
it should work

yii not showing with this $model->errors code - using composite primary key

hello friends i am using composite primary key in my yii application and i am trying to import csv file into database using save() function and i have only three rows but when i am trying to import those only one row getting insert not all. and if i am trying to access the error then nothing is showing .. but when using model->validate then it returning false.
CVarDumper::dump($model->errors,10,
CVarDumper::dump($model->attributes, 10, true);
CVarDumper::dump($model->validate(), 10, true);
what should i use to print error .. above three are not able to print error. but error is their.
regards
anil
before validating you do not have any errors, because you haven't checked for anything, validate your model and after that you can catch your errors
$model->validate();
var_dump($model->errors);

Yii framework - picking up field value from other model

I have been struggling with this, i have two models and showing data in Cgridview with one model, this model contains some id's whose values are in different table
So, i have added
'value'=> 'TblAreaoflaw::model()->FindByPk($data->typeoflaw)->areaoflaw'
which is giving this error
"Trying to get property of non-object"
Might be due to this reason that the some records doesn't exist in the TblAreaoflaw. Can't we check in this line through isset?
When i put static value, it work well, like
'value'=> 'TblAreaoflaw::model()->FindByPk(5)->areaoflaw',
Could anyone please help
thanks a lot
The error you get is because this expression TblAreaoflaw::model()->FindByPk($data->typeoflaw) is returning null. This means that you are effectively trying to get null->areaoflaw which won't work (this is what the error message "Trying to get property of non-object" clarifies).
My best guess is that $data->typeoflaw returns a non-existing primary key for the TblAreaoflaw model.
Make sure :
TblAreaoflaw is actually a model, I doubt its Areaoflaw
You have database specified primary key which is the id (5) you are passing
Try:
'value'=> '(TblAreaoflaw::model()->FindByPk($data->typeoflaw)->areaoflaw) ?
: "default or null value"'
Obviously substitute the null string to whatever you want. You may need to adjust the condition to use !empty() or similar, but see how it goes. (And if you do that or aren't using PHP 5.3, use the full ternary expression.)