CakePHP find screwed up - cakephp

I have a simple database table which looks like this: http://www.xup.in/dl,17431410/screwed.jpg/ Now I want the data using $this->find('all') in the ItemModel which looks like:
public function getList() {
$data = $this->find('all');
pr($data);
exit;
}
The output is
Array
(
[0] => Array
(
[Champion] => Array
(
[id] => 1
[name] => A
[key] => ahri
)
)
[1] => Array
(
[Champion] => Array
(
[id] => 2
[name] => A
[key] => akali
)
)
[2] => Array
(
[Champion] => Array
(
[id] => 3
[name] => A
[key] => alistar
)
)
[3] => Array
(
[Champion] => Array
(
[id] => 4
[name] => A
[key] => amumu
)
)
)
I have absolutely no idea why the name is cut at the first letter.

schema looks good to me.
did you clear the cache? the old scheme that is cached in /tmp/cache/persistent might still have something else stored.
maybe a tinyint, or char 1 etc.

Related

Group a multidimensional array by a particular value

I have a multidimensional array and am trying to group them according to the value of array properties.
I'm trying to group them by id, but I won't actually know the id beforehand. So, it's not like I can put it in a for loop and say while i < 7, because I won't know that 7 is the maximum value for the id value,
Array (
[0] => Array (
[name] => R8900
[type] => public
[id] => 1
)
[1] => Array (
[name] => R8944
[type] => public
[id] => 1
)
[2] => Array (
[name] => R8922
[type] => private
[id] => 3
)
[3] => Array (
[name] => R8816
[type] => private
[id] => 3
)
[4] => Array (
[name] => R7434
[type] => VIP
[id] => 7
)
)
What I'm hoping to produce:
Array (
[1] => Array (
[0] => Array (
[name] => R8900
[type] => public
)
[1] => Array (
[name] => R8944
[type] => public
)
)
[3] => Array (
[2] => Array (
[name] => R8922
[type] => private
)
[3] => Array (
[name] => R8816
[type] => private
)
)
[7] => Array (
[4] => Array (
[name] => R7434
[type] => VIP
)
)
)
Something as simple as:
var result:Object = {};
for each(var i:Object in input)
{
if(!result.hasOwnProperty(i.id))
{
result[i.id] = [];
}
result[i.id].push(i);
delete i.id;
}

cakephp multiple records data array fit in request data

I need to get multiple records from model, then put them into request->data for a view to render into a form with multiple input fieldsets for example name='data[Applicant][0][display_name]'. name='data[Applicant][1][display_name]'...data value goes in for each applicant.
Actually I've already done what i want, but i do not think it is a good method to do so.
Appreciate if anyone can guide me
foreach ($this->Applicant->data['Applicant'] as $key=>$item){
$data['Applicant'][] = $item['Applicant'];
}
$this->request->data = $data;//set Model to data
$this->set('data' , $this->Applicant->data);
$this->Applicant->data is the following:
Array
(
[Applicant] => Array
(
[0] => Array
(
[Applicant] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
)
[1] => Array
(
[Applicant] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
the following is the desired output (less one level):
Array
(
[Applicant] => Array
(
[0] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
[1] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
thanks
This should suffice:
$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );

Associated model is not fetched in CakePHP find

I am experiencing an odd behaviour:
In the $results['capital'] each contained model is fetched (therefore, CapitalCategory, CapitalCategory->Category and Picture) for the Capital model.
But in the $results['category'] the Picture model is not fetched (only Capital and Category are fetched) for the CapitalCategory model.
I have attached a screenshot for clarity:
What could be the problem? Where should I look? Thank you!
EDIT
Here is the output for the $results['capital'] array,
[0] => Array
(
[Capital] => Array
(
[name] => N'Djamena
[id] => 81
)
[CapitalCategory] => Array
(
[0] => Array
(
[value] => Ciad
[category_id] => 2
[capital_id] => 81
[Category] => Array
(
[name] => Ţară
)
)
)
[Picture] => Array
(
[0] => Array
(
[picture] => http://farm2.static.flickr.com/1270/879755600_126f8824db_s.jpg
[capital_id] => 81
)
)
)
and here is the output for the $results['category'] array (I've put only one record since they are similar):
[0] => Array
(
[CapitalCategory] => Array
(
[value] => America de Sud
)
[Capital] => Array
(
[name] => Asuncion
[id] => 56
)
[Category] => Array
(
[name] => Continent
[id] => 1
)
)
It looks like the problem was that CakePHP expects to have all foreign keys in the fields array. Therefore, after I have added the 'CapitalCategory.capital_id' in the fields array, the Pictures are shown. Still don't get why the first query worked while this one not, but I'm glad I figured it out.

CakePHP Set::extract parent array based on element condition

say I have an array like this:
[Option] => Array
(
[0] => Array
(
[id] => 30606
[option_description_id] => 1
[product_id] => 101512
[price] => 0
[OptionDescription] => Array
(
[id] => 1
[option_type_id] => 1
[name] => Factory
[short_name] =>
[sort_order] => 1
[OptionType] => Array
(
[id] => 1
[name] => Warranty
[seo_url] => warranty
)
)
)
[1] => Array
(
[id] => 30607
[option_description_id] => 2
[product_id] => 101512
[price] => 44.99
[OptionDescription] => Array
(
[id] => 2
[option_type_id] => 1
[name] => +2 Year Extended
[short_name] =>
[sort_order] => 2
[OptionType] => Array
(
[id] => 1
[name] => Warranty
[seo_url] => warranty
)
)
)
I want to extract the option whose OptionDescription's option_type_id ID is 1
Is there a way to do this easily with Set::extract?
Well, of course, two minutes after I post my question (after hours of trying to figure it out) I realize how easy it is.
In order to extract a parent element, you use /.. in your path syntax.
So my line for all the Options whose type_ids are 2 would be:
Set::extract('/Option/OptionDescription[option_type_id=2]/..', $product);

find value in array cakephp

Hey guys I am trying to get a value from an array in my events_controller.php file.
Event belongsTo Entity and Entity hasMany Event. I need this value to perform some other logic but im really stuck and i know it should be an easy thing to do.
I am trying to get the value of Entity.user_id from this array.
Array
(
[Event] => Array
(
[id] => 19
[entity_id] => 8
[name] => new event
[time_start] => 2011-02-26 19:09:00
[time_end] => 2011-02-26 19:09:00
[dateStart] => 0000-00-00
[dateEnd] => 0000-00-00
[description] => jgiuguygo
[ageRange] => 67
)
[Entity] => Array
(
[id] => 8
[user_id] => 14
[location_id] => 15
[type] => EVENT
)
[Eventfeedback] => Array
(
)
)
the above matrix i obtained with this code:
$value = $this->Event->read();
pr($value);
Now this is as close as I can get...
Array
(
[Entity] => Array
(
[user_id] => 14
)
[Event] => Array
(
[id] => 19
)
[Eventfeedback] => Array
(
)
)
with this code
$value = $this->Event->read('Entity.user_id');
pr($value);
An last try i got this array
Array
(
[Entity] => Array
(
[id] => 1
[user_id] => 11
[location_id] => 8
[type] => sdfsdfdsf
)
[User] => Array
(
[id] => 11
[firstName] => luis
[lastName] => pooya
[username] => admin
[password] => 94c882c8506497a9f031ca5a4db6d0143c97fe45
[role] => admin
[email] => some
)
[Location] => Array
(
[id] => 8
[name] => First Nation University of Canada
[xCoordinate] => 0
[yCoordinate] => 0
)
[Establishment] => Array
(
)
[Event] => Array
(
)
[Vmachine] => Array
(
)
)
with this code
$value = $this->Event->Entity->find('user_id');
pr($value);
Hope someone can help me out. Thanks in advance.Luis
I'm not sure I understood you correctly. But to get user_id in your examples would be like
$value = $this->Event->read('Entity.user_id');
pr($value['Entity']['user_id']);
$event = $this->Event->read();
$userId = $event['Entity']['user_id'];

Resources