Using read() in cakephp to retrieve row with array of data - cakephp

I want to know if it is possible to retrieve a row from the database using something similar to the following:
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
$this->Place->set($place);
$place_found = $this->Place->read();
}
Is there some way I can preset the data in the Place model using the array and then use Place read. I'm looking for something simple like the usual:
$this->Place->id = 7;
$place_found = $this->Place->Read();
I have also tried doing this:
$this->Place->city = blah;
$this->Place->area = foo; etc....
$place_found = $this->Place->read();
However, that also does not work.

Haven't you ever used find()?! read() only fetches a row with the ID passed.
$place_found = $this->Place->find('first', array(
'conditions' => array(
'Place.city' => $city,
'Place.area' => $area
// etc
)
));
If you need to build the conditions manually you can create a conditions array to pass like so:
$placeConditions = array();
$placeConditions['city'] = $city;
if($area) {
$placeConditions['area'] = $area;
}
$places = $this->Place->find('first', array('conditions' => $placeConditions));
I suggest you read the page I linked, you will soon find out there is never a reason to use the read() method.

In model you could do:
$this->id = 3; //place id
$this->Place->read();
Hope it helps

I think this approach is what you're looking for(with your code):
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
//$this->Place->set($place); //don't need it here I think
$place_found = $this->Place->find('all',array('conditions'=>$place));
}

You'll have to use "find()", not "read()", but - it's almost as simple as your example code and should work. (also, there's a shortcut for the array_push() I believe, but - I like to use this for readability - personal preference):
if (!empty($this->params['form'])) {
$conditions = array();
array_push($conditions, array('Place.city' => $this->params['form']['city']);
array_push($conditions, array('Place.state' => $this->params['form']['state']);
array_push($conditions, array('Place.country' => $this->params['form']['country']);
array_push($conditions, array('Place.iso' => $this->params['form']['iso']);
$this->Place->set($place);
$place_found = $this->Place->find('all', array('conditions'=>$conditions));
}

Related

Multiple collections in an array (session variable) — Property does not exist

I'am trying to fetch a session variable if the user is a guest. The variable is called "cart" and is set like this:
$product = new Collection((object) [
'product_id' => $request->pId,
'amount' => $request->amount,
'variations' => $variations
]);
Session::push('cart', $product);
Then I later fetch it:
if(Auth::check()){
$cartProducts = ShoppingCartItem::where('user_id', '=', Auth::user()->id)->get();
}else{
$cartProducts = Session::get('cart');
}
foreach($cartProducts as $product){
dd($product);
$totalAmount += $product->amount;
$totalPrice += (PriceHelper::getProductPrice($product->product->id, $product->amount));
}
The problem here is that dd($product) still outputs an array (the session variable array I assume) which means that for example $product->amount does not exist.
This is the output from dd($product):
You can either access the values using get():
foreach ($cartProducts as $product) {
$totalAmount += $product->get('amount');
$totalPrice += PriceHelper::getProductPrice($product->get('product_id'), $product->get('amount'));
}
or as an array:
foreach ($cartProducts as $product) {
$totalAmount += $product['amount'];
$totalPrice += PriceHelper::getProductPrice($product['product_id'], $product['amount']);
}
or you could use sum() on the collection instead of using foreach:
$cartProducts = collect(Session::get('cart'));
$totalAmount = $cartProducts->sum('amount');
$totalPrice = $cartProducts->sum(function ($product) {
return PriceHelper::getProductPrice($product['product_id'], $product['amount']);
});
Edit
For a quick fix if you need $product to be an object you could do something like:
$cartProducts = collect(Session::get('cart'))->map(function ($item) {
return (object)$item->toArray();
});
Hope this helps!

update multiple array values with multiple ids in laravel 5.1

My query as follows
$dueid = array('1','2');
for($n = 0; $n< count($post['percentage']); $n++) {
$due=invoiceduedates::whereIn('id',$dueid)
->update(array(
'percentage' => $post['percentage'][$n],
'amount'=>$post['pamount'][$n],
'date' => $post['date'][$n]
)
);
}
But in table,at 1st and 2nd ids the 2nd array data is getting updated.Please help me to sort it out.
I don't know what you what to get... but in this way it's normal that you get what you get. I can only sugest you to try like this:
$dueid = array('1','2');
$dues = invoiceduedates::whereIn('id',$dueid)->get();
for($n = 0; $n< count($post['percentage']); $n++) {
$due = $dues->find($dueid[$n+1]);
$due->update(array(
'percentage' => $post['percentage'][$n],
'amount'=>$post['pamount'][$n],
'date' => $post['date'][$n]
)
);
}

Symfony2: Querying for object with 2 other objects as parameters

What is the best way to do the following: I have a gameobject entity which has properties game and user. Now my method gets the game id and the user id and i want to look for the gameobject with game = object of game id and user = object of user id.
I have tried the following:
$game = $this->getDoctrine()->getRepository("xxx:Game")->find($game_id);
$user = $this->getDoctrine()->getRepository("xxx:User")->find($user_id);
$gameobject_query = $em->getRepository('xxx:Gameobject')->createQueryBuilder('g')
->where('g.game = :game AND g.user = :user')
->setParameters(array(
'game' => $game,
'user' => $user
))
->getQuery();
$gameobject = $gameobject_query->getResult();
Your advices would be appriciated :)
I think that something like the following should work:
$repository = this->getDoctrine()->getRepository('xxx:GameObject');
$queryBuilder = $repository->createQueryBuilder('go')
->innerJoin('go.game', 'g')
->innerJoin('go.user', 'u')
->where('g.id = :gameId')
->andWhere('u.id = :userId')
->setParameter('gameId', $gameId)
->setParameter('userId', $userId);
$gameObjects = $queryBuilder->getQuery()
->execute();
Alternatively, the following may also work and be more efficient: It should do the same but without needing to join to the other entities:
$repository = this->getDoctrine()->getRepository('xxx:GameObject');
$queryBuilder = $repository->createQueryBuilder('go')
->where('IDENTIY(go.game) = :gameId')
->andWhere('IDENTITY(go.user) = :userId')
->setParameter('gameId', $gameId)
->setParameter('userId', $userId);
$gameObjects = $queryBuilder->getQuery()
->execute();
Why didn't you use the findOneBy method of your Gameobject repo ?
$game = $this->getDoctrine()->getRepository("xxx:Game")->find($game_id);
$user = $this->getDoctrine()->getRepository("xxx:User")->find($user_id);
$gameobject = $em->getRepository('xxx:Gameobject')->findOneBy(array(
'game' => $game,
'user' => $user
));

How do I use fixed fields in CakeDC's csvUpload behavior in Util plugin

I am using the csvUpload behavior of the Utils plugin by CakeDC, on a CakePHP 2.2.1 install.
I have it working great it's processing a rather large csv successfully. However there are two fields in my table / Model that would be considered fixed, as they are based on ID's from from associated models that are not consistent. So I need to get these fixed values via variables which is easy enough.
So my question is, how do I use the fixed fields aspect of csvUpload? I have tried that following and many little variation, which obviously didn't work.
public function upload_csv($Id = null) {
$unique_add = 69;
if ( $this->request->is('POST') ) {
$records_count = $this->Model->find( 'count' );
try {
$fixed = array('Model' => array('random_id' => $Id, 'unique_add' => $unique_add));
$this->Model->importCSV($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
} catch (Exception $e) {
$import_errors = $this->Model->getImportErrors();
$this->set( 'import_errors', $import_errors );
$this->Session->setFlash( __('Error Importing') . ' ' . $this->request->data['Model']['CsvFile']['name'] . ', ' . __('column name mismatch.') );
$this->redirect( array('action'=>'import') );
}
$new_records_count = $this->Model->find( 'count' ) - $records_count;
$this->Session->setFlash(__('Successfully imported') . ' ' . $new_records_count . ' records from ' . $this->request->data['Model']['CsvFile']['name'] );
$this->redirect(array('plugin'=>'usermgmt', 'controller'=>'users', 'action'=>'dashboard'));
}
}
Any help would be greatly appreciated as I have only found 1 post concerning this behavior when I searching...
I made my custom method to achieve the same task. Define the following method in app\Plugin\Utils\Model\Behavior
public function getCSVData(Model &$Model, $file, $fixed = array())
{
$settings = array(
'delimiter' => ',',
'enclosure' => '"',
'hasHeader' => true
);
$this->setup($Model, $settings);
$handle = new SplFileObject($file, 'rb');
$header = $this->_getHeader($Model, $handle);
$db = $Model->getDataSource();
$db->begin($Model);
$saved = array();
$data = array();
$i = 0;
while (($row = $this->_getCSVLine($Model, $handle)) !== false)
{
foreach ($header as $k => $col)
{
// get the data field from Model.field
$col = str_replace('.', '-', trim($col));
if (strpos($col, '.') !== false)
{
list($model,$field) = explode('.', $col);
$data[$i][$model][$field] = (isset($row[$k])) ? $row[$k] : '';
}
else
{
$col = str_replace(' ','_', $col);
$data[$i][$Model->alias][$col] = (isset($row[$k])) ? $row[$k] : '';
}
}
$is_valid_row = false;
foreach($data[$i][$Model->alias] as $col => $value )
{
if(!empty($data[$i][$Model->alias][$col]))
{
$is_valid_row = true;
}
}
if($is_valid_row == true)
{
$i++;
$data = Set::merge($data, $fixed);
}
else
{
unset($data[$i]);
}
}
return $data;
}
And you can use it using:
$csv_data = $this->Model->getCSVData($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
Here $csv_data will contain an array of all of those records from the csv file which are not empty and with the fixed field in each record index.
So as I was telling Arun, I answered my own question and figured it out. I was looking to broad instead of really examining what was in front of me. I started running some debugging and figured it out.
First of all, $unique_add = 69 is seen as an int, duh. In order for it to be added to the csv it need to viewed as a string. So it simply becomes, $unique_add = '69'.
I couldn't enter the value of $Id directly into the fixed array. So I just had to perform a simple find to get the value I needed.
$needed_id = $this->Model->find('first', array(
'condition'=>array('Model.id'=>$Id)
)
);
$random_id = $needed_id['Model']['id'];
Hopefully this won't be needed to help anyone because hopefully no one else will make this silly mistake. But one plus... Now there's actually more than one post on the internet documenting the use of fixed fields in the CakeDC Utils plugin.

How to delete Model Condition?

I wrote a function which is supposed to return an array of clubs for userId. I don't know why by when I add where(Model_ClubUsers::$USERS_ID,$userId) to dsql() it doesn't set the condition and I have to use addCondition(), but I need it only in this function. Is there a way to delete the condition before function returns?
function getUserClubs($userId){
$columns = array('id',self::$NAME,self::$LOGO_NAME,self::$DESC);
$this->addRelatedEntity('club_users', 'club_users', Model_ClubUsers::$CLUBS_ID, 'inner', 'related');
$this->addField(Model_ClubUsers::$USERS_ID)->relEntity('club_users');
$aAliasedColumns = $this->getAliasedColumns($columns, $this->entity_code);
$this->addCondition(Model_ClubUsers::$USERS_ID,$userId);
$rows = $this->dsql()->field($aAliasedColumns)->do_getAll();
$aResult = array() ;
foreach($rows as $key => $value){
foreach($value as $vKey => $vVal){
if($columns[$vKey]=='id'){
$aRow['key'] = $vVal;
}else if($columns[$vKey]==self::$LOGO_NAME){
$aRow[$columns[$vKey]] = self::$CLUBS_LOGO_PATH.$vVal;
}
else {
$aRow[$columns[$vKey]] = $vVal;
}
}
$aResult[] = $aRow;
}
return $aResult;
}
Please upgrade to 4.2 where the problem is no longer present:
$x=clone $this;
$x->addCondition();
also your syntax can be probably improved with the new 4.2 features.

Resources