error updating table field with cakephp - php

i have a controller in cakephp named AdminsController. With the addadmin($id) function,as you can see,i try to update a user's role to 'admin',finding him in the database using his user id (the variable $id).
class AdminsController extends AppController {
public $helpers = array('Html','Form');
var $uses = array('Admin', 'User');
public function index() {
if(!($this->Auth->user('username') && $this->Auth->user('role') == 'admin')) {
throw new NotFoundException(__('Unauthorized access.Please login first.'));
$this->redirect(array('controller' => 'users','action' => 'login'));
}
}
public function addAdmin($id) {
$this->loadModel('User');
$this->User->id = $id;
$this->User->set('role','admin');
$this->User->save();
}
}
But this code does not work,though many other cakephp users in stackoverflow have told me that this is the way to do this..
Do you knwo what maybe goes wrong or you can help me in any case?
thank you in advance!

Try this:
public function addAdmin($id) {
$this->loadModel('User');
$this->User->id = $id;
$this->User->saveField('role','admin');
}
If that doesn't work, you are not getting the right ID.

Is 'role' is a field in your users table? And is 'admin' a valid value for that field? If so this should work:
$this->User->id = $id;
$this->User->saveField('role', 'admin');
See here. Hope this helps.

Related

How to build login system in Codeigniter 4?

I want to build a login system using Codeigniter 4.
But I face some error.
I have some data in Users_model;
Here is my some codes:
Controller/Signin.php
<?php
namespace App\Controllers;
use App\models\Users_model;
class Signin extends BaseController {
public function index() {
return view('signin/index');
}
public function authenticate() {
if ($this->exists($_POST['email'], $_POST['password']) != NULL) {
$session = session();
$session->set('email', $_POST['email']);
return $this->response->redirect(site_url('signin/profile'));
} else {
$data['msg'] = 'wrong';
return view('signin', $data);
}
}
public function profile() {
return view('signin/profile');
}
private function exists($email, $password) {
$model = new Users_model();
$account = $model->where('email', $email)->first();
if ($account != NULL) {
if (password_verify($password, $account['password'])) {
return $account;
}
}
return NULL;
}
}
Models/Users_model.php
<?php
namespace App\models;
use CodeIgniter\Model;
class Users_model extends Model {
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['first_name', 'last_name', 'email', 'password'];
}
But I face this error:
Please help me.
Or please someone suggest me a login system in another way in Codeigniter 4?
If you want to make login system, I suggest you to use validation to make user is valid and redirect to another controller or view. Then you can use filter to check that user is logged in or not and adding some routes filter to protect other controller.
First read this Codeigniter 4.0.4 documentation https://codeigniter4.github.io/userguide/libraries/validation.html
You could search anything you need there.
return view('signin', $data);
for
return view('signin/index', $data);

How to fix approval system in CakePHP

I made a blog and I can leave comments to my articles, then I made an Approve button, need to make the approve function.
I need to make it to work. I just can't, no matter what I try. I want to be able to approve or not a comment before it's posted. I have tried everything I could think of.
<?php
namespace App\Controller;
class CommentsController extends AppController
{
public function index()
{
$comments = $this->Comments->find('all');
$this->set(compact('comments'));
}
public function view($id = null)
{
$comment = $this->Comments->get($id);
$this->set(compact('comment'));
}
public function add()
{
$comment = $this->Comments->newEntity();
if ($this->request->is('post')) {
$comment = $this->Comments->patchEntity($comment, $this->request->data);
$comment->user_id = $this->Auth->user('id');
$comment->aproved = 0;
$comment->article_id = $this->request->data['article_id'];
if ($this->Comments->save($comment)) {
$this->Flash->success(__('Your comment has been saved.'));
return $this->redirect(['controller' => 'Articles', 'action' => 'view', $comment->article_id]);
}
$this->Flash->error(__('Unable to add your comment.'));
}
$this->set('comment', $comment);
}
public function aprove()
{
}
}
Try this
Your link should look like this:
$this->Html->link('approve', array('controller' => 'your-controller', 'action' => 'aprove', $comment_id, $value['Comment']['aproved']));
and your controller function is:
public function aprove($comment_id, $approve_value)
{
if(isset($comment_id) && !empty($comment_id)){
$approve = $this->Comments->find('first',array('conditions'=>array('id'=>$comment_id),'fields'=>array('Comment.id, Comment.approve')));
if(!empty($approve)){
$this->Comments->id = $approve['Comment']['id'];
$this->Comments->saveField('aproved', $approve_value);
}
}
}

How to pass and retrieve data between two controllers in Laravel

I'm trying to get to grips with Laravel and not finding the documentation any help at all. It seems to assume you know so much instead of actually walking new users through each section step by step.
I've got to the point where I need to make an internal call to another class, and sticking with MVC I can't seem to do what should be a simple thing.
My code:
class UsersController extends BaseController {
protected $layout = 'layouts.templates.page';
protected $messages;
public function getIndex()
{
$input = array('where', array('field' => 'email', 'operator' => '=', 'value' => 'tony#fluidstudiosltd.com'));
$request = Request::create('user/read', 'GET');
$users = json_decode(Route::dispatch($request)->getContent());
var_dump($users); exit;
$this->pageTitle = 'Fluid Customer Status :: Admin Users';
$this->content = View::make('layouts.admin.users');
}
}
Class UserController extends Base Controller
public function getRead()
{
$userId = (int) Request::segment(3);
if ($userId)
{
$user = User::findOrFail($userId);
return $user;
}
else
{
$users = new User;
var_dump(Input::all()); exit;
if (Input::has('where'))
{
var_dump(Input::get('where')); exit;
}
return $users->get();
}
}
Why isn't the input data from UsersController#getIndex available in UserController#getRead

How can I retrieve data using a model and pass it to be used in a controller using CodeIgniter?

From a user login form i need to use their username ($this->input->post('username')) to query against the membership table in my database so that I can retrieve the users firstname and lastname, which I believe should be done via a model called from my controller. I then need to pass that data back to my controller to use the firstname and lastname to add in to my session userdata which is set in my controller, as shown below.
$data = array(
'username' => $this->input->post('username'),
'firstname' => //need to retrieve value from database,
'lastname' => //need to retrieve value from database,
'is_logged_in' => true
);
$this->session->set_userdata($data);
Thanks,
Kristan.
In your model, make a function to retrieve the first and last name of a user:
public function get_user_details($username){
// retrieve contents from db:
// the first username here is the name of the column in your table
$query = $this->db->select('firstname, lastname')
->where('username', $username)
->get('membership');
return $query->result_array();
}
In your controller, like you were doijng, grab the POST contents in your controller:
$username = $this->input->post('username');
Then, from your controller, call your model function but save the output of that model function in a variable:
$data = $this->user_model->get_user_details($username);
After that, you can do what you were trying to do:
$this->session->set_userdata($data);
Hope that helps. I wrote this from the top of my head but it should work. I also suggest you read the CI documentation through because it really is some of the best documentation I have ever seen for a framework. Good luck.
Please check CodeIgniter user guide for model. The example of Blog controller and Blog model will answer you.
class Blogmodel extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
function insert_entry()
{
$this->title = $_POST['title']; // please read the below note
$this->content = $_POST['content'];
$this->date = time();
$this->db->insert('entries', $this);
}
function update_entry()
{
$this->title = $_POST['title'];
$this->content = $_POST['content'];
$this->date = time();
$this->db->update('entries', $this, array('id' => $_POST['id']));
}
}
class Blog_controller extends CI_Controller {
function blog()
{
$this->load->model('Blog');
$data['query'] = $this->Blog->get_last_ten_entries();
$this->load->view('blog', $data);
}
}
You first need to create a model.
class Membership_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getUser($username) {
$query = $this->db->get_where('membership', array('username' => $username));
return $query->result_array();
}
}
then in your controller, use the membership model to retrieve data. Calling this function: $this->membership_model->getUser($this->input->post('username'));

filter some data(row) in zend view

I have
1.Table:user(userId,userName,userGroup)
2.Model:userModel
3.usercontroller
there i a simple code:
Controller:
class UserController extends Zend_Controller_Action
{
public function getuser()
{
$userModel = new userModel();
$this->view->usergroup = $userModel;
}
}
Model:
class Model_UserGroupModel extends Zend_Db_Table_Abstract {
public function getuser(
{
$select = $this->select();
return $this->fetchAll($select);
}
}
view:
please tell me what code I must insert in view to only have user with specific row like user with group teacher also i use partialoop???
foreach ($this->usergroup->toArray() as $userGroup) {
echo $userGroup['row'];
}
should work
To filter rows based on a particular field like userGroup you need to do a query with where condition.
$where = $this->getAdapter->quoteInto("userGroup = ?", 'teacher');
$select = $this->select()->where($where);
So now you can get users for a particular user group.

Categories