cakephp not saving data - cakephp

For the life of me I can't figure out what's going wrong. When I debug the value of the $order->save operation, I get a 1 (which I assume to mean true). Here's what I'm doing:
$order = $this->Order->find('first', array(
'conditions' => array('Order.token' => urldecode($token))
));
debug($order);
$this->Order->id = $order['Order']['id'];
$orderData = array('Order' => array(
'id' => $order['Order']['id'],
'billing_email' => urldecode($payPalResponse['EMAIL']),
'billing_name' => urldecode($payPalResponse['PAYMENTREQUEST_0_SHIPTONAME']),
'billing_address' => urldecode($payPalResponse['PAYMENTREQUEST_0_SHIPTOSTREET']),
'billing_city' => urldecode($payPalResponse['PAYMENTREQUEST_0_SHIPTOCITY']),
'billing_state' => $payPalResponse['PAYMENTREQUEST_0_SHIPTOSTATE'],
'billing_zipcode' => $payPalResponse['PAYMENTREQUEST_0_SHIPTOZIP']
));
debug($orderData);
$this->Order->save($orderData);
And here's what I'm getting:
controllers/markets_controller.php (line 149)
Array
(
[Order] => Array
(
[id] => 13
[token] => **************
[player_id] => 1
[status_id] => 1
[timestamp] => 2012-02-15 12:09:24
[date_filled] => February 15, 2012
)
[OrderItem] => Array
(
)
)
controllers/markets_controller.php (line 161)
Array
(
[Order] => Array
(
[id] => 13
[billing_email] => *********#************.com
[billing_name] => Test User
[billing_address] => 1 Main St
[billing_city] => San Jose
[billing_state] => CA
[billing_zipcode] => 95131
)
)
Why is it telling me that it's saving but it's not?

Okay, I had tried to clear the cache before, but for some reason it didn't take. I finally cleared the cache file for the Order model, and that fixed it.

Related

Get data from array (from an API) in Laravel

It is my first time working with an external API. I have already fixed that it gives me the right output. In a controller I have the following code:
public function index() {
$api = new Wefact();
$parameters = [
];
$api_response = $api->sendRequest('product', 'list', $parameters);
print_r($api_response);
}
This shows me the array on the page. But I do not know how to use this array to get it into a foreach in the blade. The output of the print_r is as follow:
Array
(
[controller] => product
[action] => list
[status] => success
[date] => 2022-05-05T04:20:03+02:00
[totalresults] => 2
[currentresults] => 2
[offset] => 0
[products] => Array
(
[0] => Array
(
[Identifier] => 1
[ProductCode] => P0001
[ProductName] => SIM ONLY 5GB
[ProductKeyPhrase] => SIM ONLY 5GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 25
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 03:49:57
)
[1] => Array
(
[Identifier] => 2
[ProductCode] => P0002
[ProductName] => SIM ONLY 10GB
[ProductKeyPhrase] => SIM ONLY 10GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 35
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 04:03:47
)
)
)
As you can see there are two products. I want to have these in a datatable with an foreach.
Since it is my first time, I really do not know how to do this.
Anyone that can help me out?
If you want to access the array of products it would be like this
return view('your_view', [
'products' => $api_response['products']
]);

Using a variable outside PHP Foreach Loop

I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.
Here is my code:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_name = $user->user_email;
$user_email = $user->display_name;
endforeach;
So I can then use them in other areas of the page, ie:.
echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;
I've printed out the $users array which contains the following:
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 46
[user_login] => huw
[user_pass] => u7CqxbuQDvApUAF6tT.
[user_nicename] => huw
[user_email] => huw#xxxx.co.uk
[user_url] =>
[user_registered] => 2017-02-06 11:13:09
[user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
[user_status] => 0
[display_name] => Huw Daniel Rowlands
)
[ID] => 46
[caps] => Array
(
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => sssg
[1] => new_role
[2] => site_member
[3] => test_role
)
[allcaps] => Array
(
[read] => 1
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[filter] =>
)
[1] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 308
[user_login] => jeremy
[user_pass] => LLOKbkPOWQsBKUIk2qL1
[user_nicename] => magnus
[user_email] => jeremy#gmail.com
[user_url] =>
[user_registered] => 2017-05-03 19:24:42
[user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
[user_status] => 0
[display_name] => Jeremy
)
[ID] => 308
[caps] => Array
(
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => new_role
[1] => sssg
[2] => site_member
)
[allcaps] => Array
(
[read] => 1
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[filter] =>
)
)
If you want to send an email to all users with a certain role, try something like this:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email = $user->user_email;
$user_name = $user->display_name;
send_email($user_name, $user_email);
endforeach;
And then write your email function:
function send_email($name, $email) {
// Do email sending stuff here with $name & $email
}

cakephp 2 threaded not showing children

Hi i don't know what is wrong with the threaded of cakephp 2 please help
when i use threaded without condition it will show its children
$category = $this->Category->find('threaded');
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 113
[name] => Core samples
[parent_id] => 11
)
)
)
but when i use this with condition it won't show its parent
$this->Category->find('threaded',array('conditions'=>array('Category.id'=>11)));
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
)
)
)
please help why it is not showing the children when use with condition
I think the problem is because you are asking for all categories with an id of 11. Insted you should ask for all categories that are children of categorie 11.
You could use your left and right columns to get a nested array (keyword Tree behaviour). Try this:
$parentCategorie = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.id' => 11
)
);
$children = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.lft BETWEEN ? AND ?' => array($parentCategorie['Categorie']['lft'], $parentCategorie['Categorie'])['rght']
)
);
Probably the best chance you have to achive what you want is to use the children method of the Tree behavior.
You coul use it as follows
$parent=$this->Category->find('first',
array('conditions'=>array('Category.id'=>11)));
$this->Category->children($parent['Category']['id']);

cakephp contain association conditions issue

I have the folowing associations
post->primary->secondary
$results = $this->Post->find('all', array(
'conditions' => array(
'Post.post_id =' => 2,
'Primary.secondary_id !=' => null
),
'contain' => array(
'Primary' => array(
'Secondary' => array(
'conditions' => array('Secondary.short_code =' => 'code')
)
)
)
));
Returns this.
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2
[created] => 2012-10-29 09:48:29
[modified] => 2012-10-29 09:48:29
)
[Primary] => Array
(
[id] => 3
[secondary_id] => 6
[Secondary] => Array
(
[id] => 6
[short_code] => code
[created] => 2012-10-31 11:19:56
[modified] => 2012-10-31 11:20:03
)
)
)
However when I change
'conditions' => array('Secondary.short_code =' => 'code')
to
'conditions' => array('Secondary.short_code !=' => 'code')
it still returns the primary record, when I dont want it to.
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2
[created] => 2012-10-29 09:48:29
[modified] => 2012-10-29 09:48:29
)
[Primary] => Array
(
[id] => 3
[secondary_id] => 6
[Secondary] => Array
(
)
)
)
It's hard to know exactly what you're hoping to achieve, but I THINK it sounds like you're trying to limit the 'Primary' results based on conditions against the 'Secondary' model.
If that's the case, you're going to need to use joins() instead of contain().
The reason: When you use CakePHP's Containable Behavior, it actually does separate queries, then combines the results before returning the data to you. Doing it this way is great in many ways, but it does not allow you to limit parent results based on conditions against it's children.
To do that, just use joins(). (CakePHP syntax which creates MySQL JOIN)

HABTM find, how to filter the results

Ive a HABTM relationship where the output is like below, what i would like to do is have cakephp return a "list" with just the "friend.id" and "friend.company_name" and exclude the "User".
Ive spent quite a while researching how to do this and cant get it to work.
My "search method" in users controller; i think i need to use the containable behaviour but im not sure what to do. ive managed to get the "friend" results only show 2 fields but i need to get rid of the "User" results. How do i do this?
The relationship is defined as in the user model file;
var $hasAndBelongsToMany = array(
'Friend' => array(
'joinTable' => 'retailerrelationships',
'className' => 'User',
'foreignKey' => 'retailer_id',
'associationForeignKey' => 'supplier_id'
)
);
user action in user controller:
$this->User->Behaviors->attach('Containable');
$users=$this->User->find('all',array('conditions'=>array('User.id'=>$this->Auth->user('id')),'contain'=>array('Friend.id','Friend.company_name')));
$this->set('users' ,$users);
debug Output
Array
(
[0] => Array
(
[User] => Array
(
[id] => 104
[username] => admin
)
[Friend] => Array
(
[0] => Array
(
[id] => 107
[company_name] => carskitchens
[Retailerrelationship] => Array
(
[id] => 12
[retailer_id] => 104
[supplier_id] => 107
[created] => 2012-03-28 10:14:23
[modified] => 2012-03-28 10:14:23
)
)
[1] => Array
(
[id] => 112
[company_name] => mr manufacturer
[Retailerrelationship] => Array
(
[id] => 13
[retailer_id] => 104
[supplier_id] => 112
[created] => 2012-03-28 11:26:52
[modified] => 2012-03-28 11:26:52
)
)
)
)
)
You should look at the Set class.
Try this
Set::combine($results, '{n}.Friend.id', '{n}.Friend.company_name');

Resources