Laravel 5.2 seed specific data array - database

I am trying to seed my database with some specific content to get my application started. I am aware of Faker and how to use it (which I do for my Users). Now I want to fill a table with (alot) of records that are not randomly generated, so not created by Faker.
For example I want to have a table with some (lets say 30) clubs so that I can generate a few hundered users who are member of one of those 30 clubs with $faker->randomElement.
Is there another way then to type this 30 times?
$club = Club::create(array(
'name' => 'FC Barcelona',
'number' => '001',
));
Couldn't find this in the laravel (5.2) docs. Only the Faker is explained.
Thanks

What you can do is creating one array with clubs and seed them and in UsersDatabaseSeeder you can create in loop as many records you want and assign for each of them random club.
EDIT
$clubs = [['prop1'=> 'val1',], ['prop1' => 'val2']];
\DB::table('clubs')->insert($clubs);

Content::create(array(
'content_title' => 'Swiss Army Man',
'content_link' => 'www.Youtube.com',
'description' => 'This movie is .....',
'content_title' => 'Movie',
'image_path' => 'images/m1.jpg',
'year' => '2014-12-4'
));
Content::create(array(
'content_title' => 'Me Before You',
'content_link' => 'www.Youtube.com',
'description' => 'asdddddd',
'content_title' => 'Movie',
'image_path' => 'images/m2.jpg',
'year' => '2016-6-4'
));
,,, So You can create as many as u want ;)

Related

CakePHP pagination, how do I sort by the count of a contained model?

Using CakePHP 2.3, I'm retrieving data using a paginator. So, say my models are Countries having many Cities, and in my CountryController I have...
$this->Paginator->settings = [
'fields' => [
'id'
'country_name'
],
'contain' => [
'City' => [
'id'
'city_name',
'conditions' => [
'population >' => 1000000;
]
]
]
];
...which gets me a list of all counties with each row containing a list of any populous cities.
In the view I am obviously able to iterate foreach ($cities as $city) and echo $country['country_name'] etc. and also if I wish I can show a count of the contained cities by echoing count($country['City']).
Using the paginator I can sort the country results by passing back a field name in the query string, e.g. sort=country_name, but how can I get the results to sort by the count of the contained cities?
It is unfortunately not possible to sort by the count of the hasMany Table using the custom Cakephp Pagination. Your best bet is to use counterCache as described in the Docs.
You will need to have a field in country table, named as city_count. This field will be updated in the Country Table automatically by Cakephp whenever there is a save operation on city table.
Since you only want to count the cities with population > 100K. You can specify the condition in counterScope which will only update the column when condition is met.
This can be defined in your City Model as below:
class City extends AppModel {
public $belongsTo = array(
'Country' => array(
'counterCache' => true,
'counterScope' => array(
'City.population > ' => 1000000
)
)
);
}

cakephp ->set() a value in an object

I can use the cakephp set() to add a value in an object at the top level but I also need to set a value for an object inside the object and I can seem to access it. Is this possible?
I need to add business_id inside the employee object.
I though I might use $user->set->employee('business_id', '1'); but I get an error on the employee part.
object(App\Model\Entity\User) {
'email' => 'dfgdfg#sdfsdf.com',
'new_password' => 'ttt',
'confirm_password' => 'ttt',
'employee' => object(App\Model\Entity\Employee) {
'name' => 'dsfsfsdfsfd',
'email' => 'sdfsdfsdf#sdfsdf.com',
'surname' => 'sdfsdfsdfsdf',
'employee_num' => 'sdfsdfsdfsd',
'[new]' => true,
I tried it a couple of different ways and I got it work by $user->employee->set('business_id', '1');

get the products collection filter by attributes with array of values

i want to get the products collection filter by attributes with array of values in magento.
eg:get collection of products whose color from array('red','blue','green') or brand from array('x','y','z').....
note: the attribute filter will not be the intersection .I have mentioned OR.So it will be joined to form final products collection
You can try something like this :
$productCollection =Mage::getModel('catalog/product')->getCollection()
$productCollection ->addFieldToFilter(
array('color', 'brand'),
array(
array('in' => array('red', 'blue', 'green')),
array('in' => rray('x','y'))
)
);
But color is a select attribute you should pass option's id of red/blue/green values
Take a look # code to filter product by attribute by color in magento and Magento - Wiki - Using Collections in Magento
$filter = array(
'attribute' => 'color',
'in' => array('red', 'blue', 'green'), // you may need get the code id of each color
),
array(
'attribute' => 'brand',
'in' => array('x','y'),
),
));
$productCollection =Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter($filter)

Form element array cakephp

I am in need of all the arrays and subarrays for a form element. When referred cookbook,
I found only the subarrays for limited elements.
For example,
<?php
echo $this->Form->input('name', array(
'div' => array(
'id' => 'mainDiv',
'title' => 'Div Title',
'style' => 'display:block'
)
));
?>
Here in this form helper, we can get only arrays like id, title, style.But I am in need of all the possible array keys for form elements. How can we get this?
You can set common properties of any form while creating it like below which will be applicable for all form elements
echo $this->Form->create('my_form', , array(
'inputDefaults' => array(
'label' => false,
'div' => false
));
Now the label and div will not appear to any form element you gonna use, This is how you can create common set of array for all form element under it..
Hope this will help u

How to change the separators for Cakephp Form Helper for multiple checkboxes

I'm generating a bunch of checkboxes through the Form Helper.
Essenciatially I have an array with like $tests = array
$tests = array(1 => 'test', 15=>'test2');
Then I can use it like this
echo $this->Form->input('test_id', array(
'type' => 'select',
'multiple' => 'checkbox',
'div' => false,
'before' => '<li>',
'after' => '</li>',
'separator' => '</li> <li>'));
I expected it would use the div => false to take off the div of every checkbox but it only applies the options to the exterior block. Is there anyway to change all the blocks from <div class=>'checkbox'> to <li class='anything else'>
Just look at the fields it produces, then write your own simple foreach() loop and write them yourself in whatever wrapping element(s) you want.
I forget whether it's possible with Cake or not, but don't think it is. The above is what we've done before - because it's simple to write, it took us less time to write than it did to look further into it :)

Resources