Cakephp elements and for each loops - cakephp

I'm breaking my head some time now on the following cakePHP code:
This is my controller:
<?php
class HeaderController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
}
public function usp() {
return $this->set('usp', $this->Header->query('SELECT * FROM USP WHERE Actief = 1'));
}
}
And this is my element:
<?
$UNSP = $this->requestAction('header/usp');
print_r($UNSP);
foreach($UNSP['header'] as $USPs):
echo $USPs['USP']['Naam'];
endforeach;
The query works and is executed when the page loads. I get an errormessage saying Invalid argument supplied for foreach() [APP/View/Elements/header.ctp, line 9]
Can somebody please help me with this?

You are assuming, somehow, that $UNSP in the view will be populated with the view variable you set in the usp() action. This is not how requestAction() works. requestAction() can either echo out the view that you call, or return the value of the function you're calling.
Instead, since it seems that usp() doesn't have a view but instead is just used to get data, you should return it like so
public function usp() {
return $this->Header->query('SELECT * FROM USP WHERE Actief = 1');
}
Then, in your view, tell requestAction() that you want the results of the function call:
$UNSP = $this->requestAction('header/usp', array('return'));
Now $UNSP should contain the results of the query.

Related

CakePHP 'autoRender = false' does not working. Still gives missing view error

I am using cakephp 2.0.4 (Doing Changes in existing project)
My controller function is..
class PagesController extends AppController {
public function getlocations($string = ''){
$this->autoRender = false;
$aResult = array(0=>'Florida', 1=>'London');
echo json_encode($aResult);
}
}
And also I have try $this->autoLayout = $this->autoRender = false;
When I am calling this action directly in browser mysite/app/pages/getlocations it will give following error
View file "/home/mysite/public_html/testing/app/View/Pages/g.ctp" is missing.
Create a /View/Ajax/json.ctp view:
<?php
if(!empty($data)) echo json_encode($data);
Then in the action:
$this->set('data', array(0=>'Florida', 1=>'London'));
$this->layout = false;
$this->render('/Ajax/json');
You could also make it work following the Cake way.
First you have to add the following code to your routes file:
Router::parseExtensions('json');
Next, in the controller add the 'RequestHandler' to your components array and serialize your result in your getlocations function:
public $components = array('RequestHandler');
public function getlocations($string = ''){
$this->set('aResult', array(0=>'Florida', 1=>'London'));
$this->set('_serialize', 'aResult');
}
Finally, you can see the results in your browser mysite/app/pages/getlocations.json
Doing this way CakePHP will add the application/json headers automatically.
More info: http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

retrieve single record with kohana 3.3 with request->param() is NULL

i try to use kohana framework but i have a little issue wen i try to retrieve a single record from table of my database.
my table:
ads
id_ads
title_ads
description_ads
my controller:
public function action_single()
{
$ads_id = $this->request->param('id_ads');
$ads = ORM::factory('ads', $ads_id);
$view = new View('ads/single'); // load the view/ads/single.php
$view->set('ads', $ads); // set 'ads' object to view
$this->template->set('content', $view);
}
my view
<h2><?php echo $ads->title_ads; ?></h2>
<pre><?php echo $ads->description_ads; ?></pre>
when i go to to localhost/kohana/index.php/ads/single/1 the browser display nothing, where is the problem?
try this:
$ads_id = $this->request->param('id_ads');
$ads =ORM::factory('ads'->find($ads_id);
or
$ads_id = $this->request->param('id_ads');
$ads =ORM::factory('ads')->where("id","=",$ads_id)->find();
to pass parameters, i think, is better (easier) to extend Controller_Template
in this way:
class Controller_News extends Controller_Template{
...
public function action_single()
{
...
$view->ads=$ads;
$this->template->content = $view;
}
}

How cakephp2 call method another helper to current helper?

I have created helper name TicketHelper (has TicketsController), CommonHelper (no controller) and ExcelHelper (no controller). Inside TicketHelper it work fine when called any function of CommonHelper. Here example TicketHelper:
<?php
class TicketHelper extends AppHelper {
public $helpers = array('Session','Common');
public function myFunction(){
echo $this->Common->workfine();
}
?>
By the same action i called CommonHelper to ExcelHelper it produces error: Fatal error: Call to a member function workfine() on a non-object in
After many hours check, i found mistake problem with constructor
<?php
class TicketHelper extends AppHelper {
public $helpers = array('Session','Common');
public function myFunction(){
echo $this->Common->workfine();
}
//Here my problem constructor Note: function name and class name are the same
function TicketHelper (){
//My code here
}
/**
* It should be follow cakephp doc
* function __construct($id = false, $table = null, $ds = null) {
* parent::__construct($id, $table, $ds);
*
* }
*
*/
?>
You should not use a helper inside a controller. A helper is used for logic in the view. It is included in the controller but accessible in the view. For logic shared between controllers, use a component instead http://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component.
If you still want to use a helper in the controller (not recommended):
$view = new View($this);
$myHelper = $view->loadHelper('MyHelper');

cakephp call a function of another model error

I have a site develop in cakephp 2.x
I want into my controller call a function of another controller like this:
class ProductsController extends AppController {
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction(){
$this->loadModel('Unit');
$this->Unit->test();
}
}
The function test into UintController.php is this:
public function test(){
echo("test");
}
My model name are Product and Unit.
When I call the function test give me this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'prova' at line 1
In the function now is empty but give me this error.
I have tried with:
public $uses = array('Unit');
and to cancel the line with $uses.
How can I solve it?
To call a function from another controller you can use the requestAction:
Definition
"This function calls a controller’s action from any location and returns data from the action. The $url passed is a CakePHP-relative URL (/controllername/actionname/params). To pass extra data to the receiving controller action add to the $options array".
Usage
This is what your code would looks like:
class ProductsController extends AppController
{
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction() {
// Calls the action from another controller
echo $this->requestAction('/unit/test');
}
}
And then in the UnitController:
class UnitController extends AppController
{
public function test()
{
return 'Hello, I came from another controller.';
}
}
Warning
As said in the CakePHP Cookbook:
"If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model".
Best solution for you
But, the best solution for you, would be to create a function inside a model and then call from your controller, like this:
class ProductsController extends AppController {
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction() {
echo $this->Unit->test();
}
}
And in the Unit model:
class Unit extends AppModel
{
public function test(){
return 'Hello, I came from a model!';
}
}

How should I use Component in Helper?

I used AkismetComponent in AkisHelper and my AkisHelper code is:
<?php
App::uses('AppHelper', 'View/Helper');
class AkisHelper extends AppHelper {
public $helpers = array('Html');
public $components = array('Akismet');
function isValid() {
if ($this->Akismet->isKeyValid()) {
echo 'OK';
} else {
echo 'Error';
}
}
}
But this error occured:
Error: Call to a member function isKeyValid() on a non-object
File: /var/www/cakeblog/app/View/Helper/AkisHelper.php
Line: 10
Please help me to solve my problem.
Thanks
Your not supposed to be able to n_n.. It's not MVC, it's like trying to call a controller method inside the view.
However you could always pass a variable to the view in your Akismet component, something like:
class AkismetComponent extends Component {
private $controller;
public function initialize($controller) {
$this->controller = $controller;
//here I pass a variable to the view
$this->controller->set('isKeyValid',$this->isKeyValid());
}
and in your view use it like any other variable:
if(isset($isKeyValid) && $isKeyValid){
}
Anyway, if you don't want to change the component, you could still pass the variable from the controller.
The view should be used only to display the information. Helpers should be only functions to help you with that, but they are not supposed to do business logic.
Hope this helps

Resources