Sending Email For cakephp 1.3 - cakephp

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

Related

Unsupported operand types on CakePHP

I have a this
**Fatal_Error:
Error: Unsupported operand types
File: C:\wamp\www\newsletter\lib\Cake\View\Helper\FlashHelper.php
Line: 90**
every time I clicked the submit button. here's my code..
**AddsController.php //Controller**
<?php
class AddsController extends AppController {
public $helpers = array('Html','Form','Session');
public $components=array('Session');
public function index() {
if($this->request->is('post'))
{
$this->Add->create();
$this->Add->save($this->request->data);
$this->Session->setFlash('Success');
return $this->redirect(array('action'=>'index'));
}
}
}
?>
**Add.php //Model/**
<?php
App::uses('AppModel' , 'Model');
class Add extends AppModel
{
public $name = "Add";
public $useTable = 'request';
public $primaryket = 'id';
public $useDbConfig = 'default';
}
?>
**index.ctp //View/Adds/index.ctp**
<?php
echo $this->Form->create('add');
echo $this->Form->input('email');
echo $this->Form->submit('submit');
echo $this->Form->end();
?>
dbname: exercise; table: request;
goal: all inputted data must be in the db.
Thank you in advance!
Use FlashHelper to set yor flash messages, not Session->flash
https://book.cakephp.org/2.0/en/core-libraries/helpers/flash.html
// In your Controller
public $helpers = array('Html','Form','Session','Flash');
$this->Flash->set('The user has been saved.', array(
'element' => 'success'
));

I am facing problems trying to use cakephp auth component

when i try to login using auth componente i do not get any error but it doesn't login and doesn't redirect-me to any other page. Please help me. Here is the UtilizadoresController code:
<?php
App::uses('AppController', 'Controller');
/**
* Utilizadores Controller
*
* #property Utilizadore $Utilizadore
* #property PaginatorComponent $Paginator
* #property SessionComponent $Session
*/
class UtilizadoresController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers=array('Session');
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('add');
if($this->action=='add' || $this->action=='edit'){
$this->Auth->authenticate=$this->utilizadore;
}
}
function login(){
}
function logout(){
$this->redirect($this->Auth->logout());
}
}
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'utilizadores',
'action' => 'login'
)
)
);
public $helpers=array('Session');
function beforeFilter(){
$this->Auth->allow('index','view');
$this->Auth->userModel = 'utilizadore';
$this->Auth->authError='Por favor, registe-se para vizualizar a pagina.';
$this->Auth->loginError='Passord ou Username incorrectos.';
$this->Auth->loginRedirect=array('controller'=>'fichadeobras','action'=>'index');
$this->Auth->logoutRedirect=array('controller'=>'fichadeobras','action'=>'index');
$this->set('admin', $this->_isAdmin());
$this->set('logged_in', $this->_loggedIn());
$this->set('utilizadores_username',$this->_utilizadoresusername());
}
function _loggedIn(){
$logged_in = FALSE;
if($this->Auth->user()){
$logged_in =TRUE;
}
return $logged_in;
}
function _utilizadoresusername(){
$utilizadores_username=NULL;
if($this->Auth->user()){
$utilizadores_username=$this->Auth->utilizadore('username');
}
return $utilizadores_username;
}
}

Model Association Not Correct

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

validate data in an array

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.

Testing a CakePHP 2.0.5 Helper which extends FormHelper

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

Resources