Testing the numbers of rows created with Laravel - php

I'm making a functional test with laravel / Phpunit
What I expect is having 2 rows with championship_id = 123
But the content of each row may vary.
I only know how to check if a row exists :
$this->seeInDatabase('championship_settings',
['championship_id' => $championship->id,
]);
but I don't know how to check that there is 2 rows corresponding to criteria
Any idea how should I do it???

U can use seeNumRecords($expectedNumber, $table, $criteria).
<?php
$this->seeNumRecords(2, 'championship_settings', ['championship_id' => $championship->id]);
?>
param int $expectedNumber Expected number
param string $table Table name
param array $criteria Search criteria [Optional]
See http://codeception.com/docs/modules/Db#seeNumRecords

Related

How to merge and search for multiple columns in gridview?

Question:
I have merged 5 columns to display in a single column,which the result will look like
name.newname.newname2.newname3.5678
and when some field is empty consider newname3 it'll be like
name.newname.newname2.5678
Now i have to implement a search option which can search for cases like:
1.If someone choose to search for "newname" as search criteria then we should get results basing on comparision of joined string of all five columns.(name.newname.newname2.5678).
2.If someone will search "name.new" it should found "name.name2" as well
Sorting Works Just Fine,Help me with the sql query please.
MyCurrent searchmodel looks like this:
->andFilterWhere(['ilike', '"storeNames"."variantName"', $this->variantName])
->andFilterWhere(['"storeNames"."classId"' => $this->storeClassId])
->andFilterWhere(['"storeNames"."familyId"' => $this->storeFamilyId])
->andFilterWhere(['"storeNames"."platformId"' => $this->storePlatformId])
->andFilterWhere(['ilike', '"storeNames"."subFamilyName"', $this->subFamilyName])
Where columns classId,PlatformId,familyId represents integers that maps to a name and displays some names.
Its the getter function in base model,
public function getStoreName()
{
return $this->hasOne(StoreNames::className(), ['id' => 'storeNameId'])->via('transaction');
}

ZendSearch Lucene boolean query doesn't correct work with numbers

I'm new with Zend Framework 2 and ZendSearch Lucene.
My database table has three columns with integers, the first one is for the id.
In the second is the publish value (1 to 3), in the third is the category value (1 to 5).
The Table looks like this:
|id|publish|category|
|1|1|1|
|2|1|2|
|3|1|3|
|4|2|3|
|5|2|4|
I tested this with the following queries:
"publish:1" Return the correct id's 1,2,3;
"publish:2" Return the correct id's 4,5;
"publish:3" Return the incorrect id's 1,2,3,4,5; The result should be empty.
"publish:1 AND category:1" Return the correct id 1;
"publish:1 AND category:3" Return the correct id 3;
"publish:1 AND category:4" Return the correct empty result;
"publish:1 AND category:5" Return the incorrect id's 1,2,3; The result should be empty.
When a number doesn't exist, the result is not empty, it contains all rows.
Is there any option, that the result is empty when the number doesn't exist?
The default encoding is UTF-8:
\ZendSearch\Lucene\Search\QueryParser::setDefaultEncoding('UTF-8');
\ZendSearch\Lucene\Analysis\Analyzer\Analyzer::setDefault(new \ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive());
The id is unindexed, publish and category are keywords.
I think I found the answer.
When I build the query with the following code the query is "+(publish:1) +(category:1)" and this works.
$query = new \ZendSearch\Lucene\Search\Query\Boolean();
$termPublish = new \ZendSearch\Lucene\Index\Term(1, 'publish' );
$subqueryPublish = new \ZendSearch\Lucene\Search\Query\Term($termPublish);
$query->addSubquery($subqueryPublish, true); // required
$termCategory = new \ZendSearch\Lucene\Index\Term(1, 'category' );
$subqueryCategory = new \ZendSearch\Lucene\Search\Query\Term($termCategory);
$query->addSubquery($subqueryCategory, true); // required

addBetweenCondition in YII

SO I want to find that if value x is exits between the values of 2 columns or not, For that i have run the query in phpmyadmin :
Normal Approch :-
SELECT * FROM `traits_versions` WHERE 16 BETWEEN `trait_value_lower` and `trait_value_upper` and `style_id` = 1
and it is giving me fine result.But when the same approach i want to find achieve in YII that it is not running and giving the sql error :
YII apprroch :-
$details = array();
$criteria = new CDbCriteria();
$criteria->addCondition('style_id='.$style_id);
$criteria->addCondition('version='.$version);
$criteria->addBetweenCondition($style_contribution,$this->trait_value_lower,$this->trait_value_upper);
$trait_details= $this->find($criteria);
When i debug the query in log than it shows in case of yii :
SELECT * FROM `traits_versions` `t` WHERE ((style_id=1) AND (version=1)) AND (16 BETWEEN NULL AND NULL) LIMIT 1
Why it is giving NULL value in query while i'm passing the name of the column in it.
So please guide me where i'm going wrong in yii.
Add compare condition like below
$criteria->compare('trait_value_lower', 16, false, '>');
$criteria->compare('trait_value_upper',16, false, '<');
instead of between condition
$criteria->addBetweenCondition($style_contribution,$this->trait_value_lower,$this->trait_value_upper);
because between condition will apply on one column as per Yii doc.
public static addBetweenCondition(string $column, string $valueStart, string $valueEnd, string $operator='AND')

How do you populate a dropdown box using data from another model so that all rows are taken as options in yii?

I'm using version 1.1.14 of yii.
My VIEW file has
<?php echo $form->dropDownList($model,'estado', CHtml::listData(Estado::model()->findAll(), 'id', 'estado')); ?>
I have a model called Estado which was generated from a table with only 2 fields ID as PK and estado where I have my data. Which has 3 rows Active, Inactive, Prospecting.
So far the code only shows the last row of that table, ignoring the first 2.
What am I doing wrong?
for the dropdown list you can pass a normal array :
$data = array(
'number1',
'number2',
'number3',
);
or an array with key => value
$data = array(
7 => 'number7',
2 =>'number2',
4 =>'number4',
);
Chtml::listData() will only help you make that array avalable for the function
however if you need to make a combination of models ( or arrays) you have to do that manually using array concatenation functions such as CMap::mergeArray()

Yii relations using variables of the active model

I have two rows in one of my tables which look like:
id product_id target_product_id description type
1 206587 456 sdfgdfgdfg 0
2 456 206587 fgdgfhghfgfdsgfdghfghfsd 0
When viewing the model for the row with id 1 I wish to get the second row based on where the product_id and the target_product_id are inversed. So I made a relation of:
'linked_product_relation' => array(self::HAS_ONE, 'Accessory', '',
'on'=>'linked_product_relation.target_product_id = product_id
AND link_product_relation.product_id = target_product_id')
However, it seems to only ever return null. I have checked that link_product_relation links to the table, and I get no SQL error, just a null return. If I use the relation with only link_product_relation.product_id = product_id though I do actually get a response, but only the row I am currently looking at. I seem to be missing something.
How can I get my desired output?
Edit
When I add a function to replace the relation:
function getTwinned(){
$a=Accessory::model()->findByAttributes(array('target_product_id'=>$this->product_id, 'product_id'=>$this->target_product_id));
if($a===null)
return null;
else
return $a;
}
It works perfectly.
You did not specify a foreign key ('' in your code). Try something like this:
'linked' => array(self::BELONGS_TO, 'Accessory', array(
'target_product_id'=>'product_id',
'product_id' => 'target_product_id',
)),
For more information also read the manual on this topic here and here.

Categories