reading and updating record and doing addition - cakephp

I have a field called force. Its by default a null field. I want to add 1 everytime I run an if block. here is my code sample
if($somecondition){
$array = array();
$array[] = $this->Model->read(null, 1);
$array['force']++;
$this->Model->updateAll(array('Model.complete' => 1, 'Model.force' => $array['force']),array('Model.completed IS NULL'));
}
I am getting an error of undefined variable $array. Not sure why.

it seems you are very new to Cake (and maybe even PHP)
if($somecondition){
$array = $this->Model->read('force',1);
if($array['Model']['force']===NULL)$array['Model']['force'] = 0;
$array['Model']['force']++;
$array['Model']['complete']=1;
$this->Model->save($array);
}
It you can, change the default value of 'force' to 0 in the DB, so you don't have to check for that here.

You should initialize $array['force'] to 0 before incrementing it.

Related

unexpected result in a query in laravel

I’m a beginner in Laravel but have a problem at first. I wrote this query and I’m waiting for Sonya Bins as result but unexpectedly I see ["Sonya Bins"]. what’s the problem?
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
return view('products',compact('articles'));
});
pluck will return array if you want to get only single value then use value
// will return array
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
//will return string
$articles=DB::table('users')->where('id','2')->value('name');
// output Sonya Bins
here is an example from the documentation:
if you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
Read more about it here
Hope it helps.
Thanks
pluck() used to return a String before Laravel 5.1, but now it returns an array.
The alternative for that behavior now is value()
Try this:
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->value('name');
return view('products',compact('articles'));
});
I think it's easier to use the Model + find function + value function.
Route::get('products', function () {
$articles = User::find(2)->value('name');
return view('products',compact('articles'));
});
pluck will return the collection.
I think id is your primary key.
You can just get the first record, and call its attribute's name:
DB::table('users')->where('id','2')->first()->name;
or
DB::table('users')->find(2)->name;
First thing is that you used invalid name for what you pass to view - you don't pass articles but user name.
Second thing is that you use get method to get results instead of first (or find) - you probably expect there is only single user with id = 2.
So to sum up you should use:
$userName = DB::table('users')->find(2)->name;
return view('products',compact('userName'));
Of course above code is for case when you are 100% sure there is user with id = 2 in database. If it might happen there won't be such user, you should use construction like this:
$userName = optional(DB::table('users')->find(2))->name;
($userName will be null if there is no such record)
or
$userName = optional(DB::table('users')->find(2))->name ?? 'No user';
in case you want to use custom string.

cakephp - using create() not working as expected

I have a product controller and when I'm saving a new product I want to save some records to another related controller to make a record of what categories the product is associated with.
My code I'm using is:
$this->Product->create();
if ($this->Product->save($this->request->data)) {
$newProductId = $this->Product->getInsertID();
//associate products with categories
foreach($categoriesToSave as $key=>$value) {
$saveArray['CategoriesProduct'] = array('category_id'=>$value, 'product_id'=>$newProductId);
$this->Product->CategoriesProduct->create();
$this->Product->CategoriesProduct->save($saveArray);
$this->Product->CategoriesProduct->clear();
}
}
For some reason though, even if $categoriesToSave has 10 items in it, only the very last one is being saved. So it's obviuosly creating only the one new CategoriesProduct item and saving each record over the top of the last instead of create()-ing a new one.
Can anyone explain what I'm doing wrong and how I can make this work?
The way I would do it would be like this:
//Add a counter
$c = 0
foreach($categoriesToSave as $key=>$value) {
$saveArray[$c]['CategoriesProduct']['category_id'] = $value;
$saveArray[$c]['CategoriesProduct']['product_id'] = $newProductId;
$c++;
}
$this->Product->CategoriesProduct->saveMany($saveArray);
I don't think that is the only way to do it but that should work just fine.
Good luck!

Use twitchAPI with cake

since a few hours i'm trying to implement the twitchAPI in my cake projet. a long time ago i made this little script in basic php.
$channelName = "gamespot";
$json_file = #file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$channelName}", 0, null, null);
$json_array = json_decode($json_file, true);
#$json_array[0] && $json_array[0]['name'] == "live_user_{$channelName}";
#$title = $json_array[0]['channel']['status'];
#$game = $json_array[0]['meta_game'];
#$chanel_view = $json_array[0]['channel_count'];
#$totalchanelview = $json_array[0]['channel_view_count'];
but i don't know how to add this lines on my controller
For know i've just find this
public function twitch() {
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=manvsgame');
$twitch = json_decode($json);
$totalchanelview = $twitch[0]['channel_view_count'];
$this->set('twitch', 'totalchanelview');
}
but of course i've this error
Fatal error: Cannot use object of type stdClass as array in /Users/*/Desktop/Websites/**/app/Controller/UsersController.php on line 29
anyone can explain to me how i can use this API?
thanks in advance and have a nice day/night :)
okey first thanks to help me. i still have a little "logic problem"
my function is something like that:
public function twitch() {
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=gamespot');
$twitch = json_decode($json, true);
$this->set('json', $twitch);
}
but know, what can I write to my view to see my informations (like the title of my stream for exemple.
I test with
echo $twitch[0]['title']; (it's my line 1)
bit i've this error
Notice (8): Undefined variable: twitch [APP/View/Users/admin_dashboard.ctp, line 1]
$twitch = json_decode($json, true); // add true param
$twitch[0]['channel_view_count'];
adding true returns the data as an associated array instead

Prestashop 1.4.3 I need to get a database value and use it in MailAlerts module

I have asked this question on the Prestashop forum, but haven't had a reply as of yet.
I need to be able to add a sagepay code to the new order email used in mailalert module.
What I have is;
// Filling-in vars for email
$template = 'new_order';
$subject = $this->l('New order');
$spvtxc = Db::getInstance()->ExecuteS("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '$order->id_cart'");
...
$templateVars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
...
'{sagepay_no}' => $spvtxc,
...
);
Every time i test a transaction the $spvtxc returns 'ARRAY'.
I have tried;
$spvtxc = '5';
As Expected this returns 5 as the sagepay number, so I'm confident the variables are being called and added to the email.
And I tried;
$spvtxc = Db::getInstance()->ExecuteS("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '2'");
So this should set $spvtxc to the value that is definatly there (i manually added it in the database), but this still returned 'ARRAY'.
If anyone can point out what I have missed, it is greatly appriceated.
As I was only needing to return a single value, I should have used getValue function instead of ExecuteS.
$spvtxc = Db::getInstance()->getValue("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '$order->id_cart'");
This returned the value.

CAKEPHP create unique code on entries in database

ok sorry, wrote too little info here and i have done some research:
The question: What is the easiest way to write a model function that creates an unique code (5 characters) for each row in a cake model/table that doesnt already have a code?
//create a unique code
//set the random id length
function generateCode()
{
while($i > 0)
{
$random_id_length = 7;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));
//finally I take the first 7 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);
//check if the code is unique
$i = $this->checkCode($rnd_id);
}
return $rnd_id;
}
//check to see if the code isnt already in the database
function checkCode($code)
{
$conditions = array("uniqueCode" => $code));
$result = $this->Visitors->count('all', array('conditions' => $conditions));
return $result;
}
//check how many has a code
function check()
{
$conditions = array("NOT" => array ("uniqueCode" => null));
$result = $this->Visitors->count('all', array('conditions' => $conditions));
return $result;
}
function update()
{
//update until everyone has a code
while( $i > 0)
{
$this->generateCode()
// get next visitors that DONT have a code.
$conditions = array ("uniqueCode" => null));
$visitor = $this->Visitors->find('first', array('conditions' => $conditions));
$i = $this->check();
}
echo 'Done';
}
Cant make it work properly and it must be an easier way?
How about an id column? :]
If You want it to be like a hash thing, I think use this:
substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,;:!?.$/*-+&#_+;./*&?$-!,"), 0, 5);
and you shouldn't worry about checking whether it's unique. Or do it, so create a hash like that and try to find it in the table, if it's not in there - your good to go.
in your Model, you can do something like this
<?php
function beforeSave() {
if (empty($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['id'] = base_convert(uniqid(), 16, 36);
}
return $this->data;
}
?>
This will generate a unique id of 10 chars. You can figure out how to create a unique id of 5 chars, maybe converting the string to a higher base, but I find it difficult because you'll have to make your own function, base_convert has a limit of 36
One thing to think about is the length of the code. A five character code, assuming you use upper and lower case characters in it, has 380,204,032 possible unique codes. If you create these codes truly randomly, you'll rather soon hit the point where you're creating duplicates. There's a 50% chance of collision after you have generated 22,959 codes.
So, since for each new code you need to check whether it already exists or not,
(BTW, a much more elegant check is this):
do {
$code = /* create code */;
} while (!$this->isUnique(array('code' => $code)));
over time you'll hit more and more already existing codes, slowing down the generation of each code, to the point where you may have to loop 100 or more times to find a unique code (99% chance of collision at only 59,177 codes).
If you're just talking about a few thousand records max, I wouldn't worry about it. If your dataset could/is supposed to grow beyond that though, you should think either about a different scheme for your codes (UUIDs come to mind), generate codes sequentially or pre-generate a list of all possible codes, shuffle it and use up codes one by one.
There are a gazillion ways to do this and all you have to do is google the appropriate words. I found it in the PHP manual, so that would be a good place to start.
To make it unique, it would be based on date and would incorporate some sort of hashing.
Do some searching, reading and learning.

Resources