Fzaninotto/Faker IF statement when generating fake data - laravel

I am currently trying to fake seed quite a vast table of mine. I am currently using Laravel 4.1.
I have one column which is set to NULL, because it only has to be filled if the previous column is set to '1', for example.
I currently have:
$alerts =
[[
'user_id' => $i,
'reference' => $faker->sentence($nbWords = 1),
'type' => rand(1,2),
'category' => rand(1,4),
'headline' => $faker->sentence($nbWords = 2),
'description' => $faker->realText(150)
]];
So, should 'type' be '1', I want the following two fields 'range_to' and 'range_from' to be then faked.
Is this possible logically? Any pointers would be hugely appreciated.

Split your carry using if statement
$alerts =
[[
'user_id' => $i,
'reference' => $faker->sentence($nbWords = 1),
'type' => rand(1,2),
'category' => rand(1,4),
'headline' => $faker->sentence($nbWords = 2),
'description' => $faker->realText(150)
]];
foreach($alerts as $key=>$alert){
if( $alert['type'] != 1 ){
$alerts[$key]['category'] = $alerts[$key]['headline'] = $alerts[$key]['description'] = NULL ;
}
}
So you set your alerts as you want, then you loop on them and apply the condition you want if it exist then you edit the array.
in above example it will loop $alerts and for each array if ['type'] not equal 1 it will set category,headline and description of that array to NULL..

Related

Row inserted but number not

I trying to insert a row in codeigniter and row inserted.
Problem is all row inserted properly but in sql bighint(11) field inserted 0.
I checked properly in array value given.
$data = [
'sku' => $POST['rec1'],
'pruch_price' => $POST['rec2'],
'sell_price' => $POST['rec3']
];
$model->insert ($data);
you should use $_POST[] instead of $POST.
But better yet, don't send $_POST directly to the models, instead use the post request provided by Codeigniter 4.
$data = [
'sku' => $this->request->getPost('rec1'),
'pruch_price' => $this->request->getPost('rec2'),
'sell_price' => $this->request->getPost('rec3')
];
$model->insert($data);

how to sort data in Laravel Eloquent query, based on a specific field

I have this Eloquent query:
$events = Product::forCard()->latest()->take(setting('storefront_recent_products_section_total_products', 10))->get();
I want to sort this record in ascending order by start_event. I tried to do this, but I have some issues because data come from multiple tables.
Array
(
[0] => Array
(
[price] => Modules\Support\Money Object
(
[amount:Modules\Support\Money:private] =>
[currency:Modules\Support\Money:private] => USD
)
[special_price] =>
[selling_price] => Modules\Support\Money Object
(
[amount:Modules\Support\Money:private] =>
[currency:Modules\Support\Money:private] => USD
)
[special_price_start] =>
[special_price_end] =>
[options_count] => 0
[id] => 40
[slug] => -YvHm5m1m
[in_stock] => 1
[new_from] =>
[new_to] =>
[name] => Race4
[description] =>
[organizer] =>
[short_description] =>
[address] =>
[city] => vehari
[state] => pakistan
[zip_code] =>
[lat] =>
[lng] =>
[start_event] => 2020-01-13 00:00:00
[end_event] =>
[translations] => Array
(
[0] => Array
(
[id] => 33
[product_id] => 40
[locale] => en
[name] => Race4
[city] => vehari
[state] => pakistan
[start_event] => 2020-01-13 00:00:00
)
)
[files] => Array
(
)
)
You should be able to order is ascending something similiar to this:
$events = Product::forCard()->latest()
->take(setting('storefront_recent_products_section_total_products', 10))
->orderBy('start_event', 'asc');
Basically you can tell using the method orderBy() which column shall be used to order and the type of ordering, in this case ascending.
Laravel makes it easy and absolutely no brainer to sort data.
$events = Product::forCard()->latest()->take(setting('storefront_recent_products_section_total_products', 10))
->orderBy('start_event')->get();
Where start_event is the column you want t use in sorting.
There is a better approach, as the above code will automatically sort the returned data in an ascending order, but you can explicitly tell it what to do.
Just modify the last part of the code with:
orderBy('start_event', 'ASC')->get(); /** If you want Ascending OR; **/
orderBy('start_event', 'DESC')->get(); /** If you want Descending. **/

How to insert the array values in database using for each loop

I have array i want insert into the values in database using codeigniter, i don't know how to insert , i trying but i am not able to get the answer
My model
print_r($subjectHandling);
Array
(
[subjectId] => Array
(
[0] => 1
[1] => 2
)
)
now i want insert the values in database in this values.
I am trying like this
foreach($subjectHandling as $key=>$value) {
$reg_dat = array(
'statffId' => '1',
'subjectId' => $value,
);
$this->db->insert("subject_handling" , $reg_dat);
}
I m getting error ** Array to string conversion** , so how do this. i want to insert two roes in databse
This should work
$subjectHandling['subjectId'] = array(1, 2);
$reg_dat = array();
foreach($subjectHandling['subjectId'] as $key => $value) {
$reg_dat[] = array('staffId'=> 1, 'subjectId' => $value);
}
$this->db->insert_batch('subject_handling', $reg_dat);
https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data

Magento: Combine eq and is null in addAttributeToFilter

I want to combine with OR "is null" and "eq" => array()
$collection->addAttributeToFilter('attributename', array('is' => null));
$collection->addAttributeToFilter('attributename', array(115,116));
But this does not work.
To use addAttributeToFilter() with an OR condition you can pass an array of arrays like this:
$collection->addAttributeToFilter(
array(
array(
'attribute' => 'name_of_attribute_1',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'name_of_attribute_2',
'in' => array(115, 116)
)
)
);
The definition of filtering against NULL keywords may look somewhat confusing at first glance, but it's plain simple, once you get used to it.
Magento supports two filter types of type string, namely 'null' and 'notnull', for comparisions against NULL keywords.
Be aware, that even though you need to pass key=>value pairs (because an associative array is used), the value will be always ignored, when filter type 'null' or 'notnull' is used.
Example
var_dump(
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter(
array(
array(
'attribute' => 'description',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'sku',
'in' => array(115, 116)
)
)
)
->getSelectSql(true)
);
The output of the dump may vary depending on your shop configuration (attribute ids, count of stores, etc.), but the OR logic in the WHERE clause should always remain the same:
SELECT
`e`.*,
IFNULL(_table_description.value, _table_description_default.value) AS `description`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_entity_text` AS `_table_description_default` ON
(_table_description_default.entity_id = e.entity_id) AND
(_table_description_default.attribute_id='57') AND
_table_description_default.store_id=0
LEFT JOIN
`catalog_product_entity_text` AS `_table_description` ON
(_table_description.entity_id = e.entity_id) AND
(_table_description.attribute_id='57') AND
(_table_description.store_id='1')
WHERE (
(IFNULL(_table_description.value, _table_description_default.value) is NULL) OR
(e.sku in (115, 116))
)
I have product attribute is_expired with Yes and No value and i want only those product have NO value.below query work for me that get record that have neq 1 and not null.
Combine not equal to and null condition WITH OR.
$collection->addAttributeToFilter('attribue_name', array('or'=> array(
0 => array( 'neq' => '1'),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
You can changed neq with eq.
To use addAttributeToFilter with an OR condition and with left join
you can do something like that:
$collection->addAttributeToFilter(
array(
array('attribute' => 'name_of_attribute_1','null' => 'this_value_doesnt_matter'),
array('attribute' => 'name_of_attribute_2','in' => array(115, 116))
)
'',
'left'
);

Multiple level ordering in Yii

This is my CActiveDataProvider
$dataProvider = new CActiveDataProvider ('MyTable', array (
'pagination' => array (
'PageSize' => 4,
),
'criteria' => array (
'condition' => 'from_user_id='.$user->id,
'order' => 'date DESC',
),
));
My question is: I would like to have this CActiveDataProvider ordered first by date (as the posted code does), and in a second level, within those with the same date, order by a second criteria. Is this possible?
Maybe I'm not understanding you correctly, but won't the following work? :
'order' => 'date DESC, name DESC',

Resources