I am creating a CakePHP helper which extends from FormHelper:
App::uses('FormHelper', 'View/Helper');
class MyFormHelper extends FormHelper{
public function wysiwyg($fieldName, $options = array()){
return parent::textarea('Model.field');
}
}
Here is my TestCase:
App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('MyFormHelper', 'View/Helper');
class MyFormHelperTest extends CakeTestCase {
public $helper = null;
public function setUp() {
parent::setUp();
$Controller = new Controller();
$View = new View($Controller);
$this->helper = new MyFormHelper($View);
}
public function testWysiwyg() {
$result = $this->helper->wysiwyg('Model.field');
$expected = array(
'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
'/textarea',
);
$this->assertTags($result, $expected);
}
}
When I run the test, I have a PHPUNIT_FRAMEWORK_ERROR_NOTICE
Trying to get property of non-object
I know that the problem comes from my helper:
return parent::textarea('Model.field');
I have no idea how to fix this.
Thanks in advance :)
To overwrite a HtmlHelper method in Cake 2.0 you can simply:
Create your OwnHelper class containing for example a link method, which extends HtmlHelper, in AppController specify:
$helpers = array('Html' => array('className' => 'OwnHelper'));
via ADmad
Related
I just created 2 models UsersModel and UserpicsModel and try to get both records but it is returning Error: Call to a member function find() on a non-object.
//UsersModel.php
class Users extends AppModel
{
public $hasMany = array(
'Userpics' => array(
'className' => 'Userpics'
)
);
}
//UserpicsMdoel.php
class Userpics extends AppModel
{
public $belongsTo = array(
'Users' => array(
'className' => 'Users',
'foreignKey' => 'uid'
)
);
}
//RecipesController.php
class RecipesController extends AppController {
public $uses =array('Users','Userpics');
public function view() {
$users = $this->Users->Userpics->find('all');
print('<pre>');
print_r($users);
print('<pre>');
exit;
}
}
first: you are not following cake conventions: models should be singular and not plural.
But the actual problem here is that the model files names are wrong: if you still want to use your conventions then the name for the Users model should be Users.php and not UsersModel.php
the same for Userpics
read this useful answer about how to debug this kind of error.
But if you decide to use the cake naming conventions (and I strongly suggest you to do so) consider doing the following:
//User.php (table users)
class User extends AppModel
{
....
}
//UserPic.php (table user_pics)
class UserPic extends AppModel
{
....
}
//RecipesController.php
class RecipesController extends AppController {
public $uses =array('User','UserPic');
public function view() {
$users = $this->User->UserPic->find('all');
print('<pre>');
print_r($users);
print('<pre>');
exit;
}
}
//UsersModel.php
class Users extends AppModel
{
public $hasMany = array(
'Userpics' => array(
'className' => 'Userpics',
'foreignKey' => 'uid'
)
);
}
I'm using the excellent CakeDC Tags plugin on my Solutions model:
class Solution extends AppModel {
public $actsAs = array(
'Tags.Taggable',
'Search.Searchable',
);
}
I have a SolutionsController::search() method:
App::uses('AppController', 'Controller');
class SolutionsController extends AppController {
public $components = array(
'Paginator',
'Search.Prg',
);
public $presetVars = true; // using the model configuration ('Search' plugin)
public function search() {
$this->Prg->commonProcess();
$this->Paginator->settings['conditions'] = $this->Solution->parseCriteria($this->Prg->parsedParams());
$solutions = $this->Paginator->paginate();
if (!empty($solutions)) {
$this->Session->setFlash('Solutions found');
} else {
$this->Session->setFlash('No solutions found');
}
$this->set('solutions', $solutions);
$this->render('index');
}
I'm trying to write a test for this method:
App::uses('SolutionsController', 'Controller');
class SolutionsControllerTest extends ControllerTestCase {
public $fixtures = array(
'app.solution',
'plugin.tags.tag'
);
public function testSearchForOneResultShouldOutputText() {
$data = array('search' => 'fiery');
$result = $this->Solution->search($data);
debug($result);
$expected = array(
'id' => 3,
'name' => 'fiery-colored horse',
'shortdesc' => 'war',
'body' => 'it was granted to the one seated on it..',
'category_id' => 3,
'created_by' => 1,
'modified_by' => 1,
'created' => '2014-02-14 21:28:46',
'modified' => '2014-02-14 21:28:46'
);
$this->assertContains($expected);
}
}
I'm getting this error when running the test:
Missing Database Table
Error: Table tags for model Tag was not found in datasource test.
I've tried copying the plugin Tag fixture to my app Test/fixtures folder and including it as an app fixture. I can't get my test to run. How do I get my test to see the tags fixture from app\Plugin\tags\Test\Fixture\TagFixture.php and run?
Problem turned out to be my SolutionsFixture, which imported the table schema and the records, then also included a $records array (oops). Re-baking this fixture to import neither schema nor records resolved the error.
I am few months old with CakePHP. This is first time I am trying CakePhp Association. I assume I am following almost all instruction but still my model doesn't seem to work.
This is simple 'User' and 'Profile' Model. Table Structure: User:
- id (Primary Key)
- name
Profile:
- id (primary key)
- role
- user_id (Reference Key)
Models: User:
class UserModel extends AppModel{
var $name = 'User';
public $hasOne = array(
'Profile'
);
}
Profile:
class ProfileModel extends AppModel{
var $name = 'Profile';
public $belongsTo = array('User'); }
Controller: Users:
lass UsersController extends AppController{
var $name = 'Users';
public $scaffold;
function index(){
$user = $this->User->find('all'); //HERE I expect to get User and Profiles data
pr($user);
}
}
Profiles:
class ProfilesController extends AppController{
var $name = 'Profiles';
public $scaffold;
function index(){
$profile = $this->Profile->find('all');
pr($profile);
}
}
If I run users: /localhost/test_php_apps/users/ I get:
Array (
[0] => Array
(
[User] => Array
(
[id] => 1
[name] => rohini
)
)
)
I am wondering why 'Profile' data is not shown. I have manually added records in tables.
Further if I try in UsersController: $user = $this->User->Profile->find('all'); I get the following error:
Call to a member function find() on a non-object
My guess is something is wrong with setting up Associations. But not sure what is messing things up.
I know this is very basic question, but even after reading 10 to 15 related cases I don't seem to find the answer.
Any help will be much appreciated.
Do me a favor, change
class UserModel extends AppModel
to
class User extends AppModel
and
class ProfileModel extends AppModel
to
class Profile extends AppModel
and see if that helps when doing $user = $this->User->Profile->find('all'); in the UsersController.
Also, is
public $actsAs = array('Containable');
not
public $actAs = 'Containable';
not actAs. See if that helps any. If not, it would be helpful to know your cake version.
See here for containable and this for naming model conventions.
If you bake this things is made your life easier.
Models: User:
class User extends AppModel{
var $name = 'User';
public $hasMany = array(
'Profile'=>array(
'className' => 'Profile',
'foreignKey' => 'user_id',)
);
}
Profile:
class Profile extends AppModel{
var $name = 'Profile';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
)
);
}
Controller: Users:
class UsersController extends AppController{
var $name = 'User';
public function index(){
$users = $this->User->find('all');
var_dump($users);
}
}
Controller Profile:
class ProfilesController extends AppController{
var $name = 'Profile';
public function index(){
$user_id = 2;
$users = $this->Profile->find('all', array('conditions'=>array('Profile.user_id'=>$user_id)));
var_dump($users);
}
}
add the following lines in your usermodel
public $useTable = 'YOUR USERTABLENAME';
and change
public $hasOne = array(
'Profile'
);
to public $hasOne = 'Profile';
in your profilmodel add the same.
public $useTable = 'YOUR PROFILETABLENAME';
and change
public $belongsTo = array('User');
to
public $belongsTo = array('User' => array('className' => 'User',
'foreignKey' => 'user_id'));
in your userscontontroller add this
public $uses = array('User','Profile');
normally it should work now when you try the query
$u = $this->User->find('all',array('contain' => array('Profile'));
or
$u = $this->User->find('all',array('recursive' => 2));
but it also should work if you write only:
$u $this->User->find('all');
regards
in controller :
<?php
App::uses('CakeEmail', 'Network/Email');
class MessagesController extends AppController
{
public $uses = array();
public function send()
{
if (!empty($this->request->data) )
{
$email = new CakeEmail();
$email->from(array('jerold#ballo.com.ph' => 'Jerold Ballo'));
$email->to($this->Email->data['to']);
$email->subject($this->Email->data['subject']);
if ($email->send($this->Email->data['message'])) {
$this->Session->setFlash(__('Email From me'), 'default', array('class' => 'success'));
}
}
}
}
?>
and i got this
Fatal error: Call to undefined method App::uses() in C:\xampp\htdocs\reservation\controllers\messages_controller.php on line 3
Please Help me....
Remove App::uses('CakeEmail', 'Network/Email');
Try
class MessagesController extends AppController
{
public $components = array('Email');
...
You can now use $this->Email the way you have it in the code
Can the validates method validate user-defined arrays? for example:
Model:
App::uses('AppModel', 'Model');
class Recipe extends AppModel {
public $validate = array(
'price' => 'numeric'
);
}
And in Controller:
App::uses('AppController', 'Controller');
class RecipesController extends AppController {
public function add() {
if($this->request->is('post') && $this->request->data){
$data = array('price' => $this->request->data['myprice']);
$this->Reservation->validates($data); //validate the $data array
}
else{
throw new NotFoundException();
}
}
}
for manually validate you should try this :
$this->Reservation->set( $data);
if($this->Reservation->validates(){
//your code
}else{
$this->validateErrors($this->Reservation);
}
in your controller you can work with a fieldlist like this:
if ($this->Model->validates(array(
'fieldList' => array(
'reason',
'name',
'message',
)
))) {
}
Hope thats what youre looking for.