jQuery post call with codeigniter fails - php

I am trying to delete the element with post jquery request:
$(function(){
//The element
// <a data-id="39" data-toggle="modal" href="#delete"><i class="icon-trash"></i>
Delete</a>
$('.delete').on('click',function(){
var id= $(this).attr('data-id');
$.post('task/delete', { id: id }, function(data) {
alert('Task deleted!');
})
.success(function(){ alert('Task deleted!'); })
.complete(function(){ alert('Task completed!'); })
.error(function(){ alert('Error was found!'); });
})
});
My controller is called task and the function inside it is called delete.
class Task extends CI_Controller {
public function delete()
{
$this->load->model('tasks_model','task_delete');
$this->task_delete-> deleteTask($_POST['id']);
}
}
The model is quite simple.. it simply deletes the record.
public function deleteTask($task_id)
{
$task_id = mysql_real_escape_string($task_id);
$this->db->query("DELETE FROM tasks WHERE task_id = ?", array($task_id));
}
I get two messages..one is error and one is delete...
Another thing that I want to avoid is someone posting the id to the controller task .. which will delete the records one by one automatically, is there a way to avoid this too?

Your controller method should look something like this
public function delete()
{
// Do user validation here
$this->load->model('tasks_model');
$this->tasks_model->task_delete($this->input->post('id'));
return "task deleted";
}
And you models method like this
public function task_delete($task_id)
{
$this->db->where('task_id', $task_id);
$this->db->delete('tasks');
}
As I would recommend validating the user where the comment says you should, and using CI's active record library where you can to increase portability to a different DB.
Edit
To show PHP errors (and maybe MySQL errors if they are turned on).
error_reporting(E_ALL);
To ensure the task is deleted, do this (which is not as efficient).
public function task_delete($task_id)
{
$this->db->where('task_id', $task_id);
$this->db->delete('tasks');
$this->db->from('tasks');
$this->db->where('task_id', $task_id);
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
return FALSE;
}
else return TRUE;
}
And then do something with the returned boolean in the controller.

You haven't used the function name in your controller it should be:
$this->task_delete->deleteTask($id);

To avoid someone posting all IDs and deleting all tasks, you should only delete tasks which belong to this user.
I'm guessing you save each task so it belongs to a specific user, right? So in your delete query, you can make it so you run a DELETE WHERE task_id={task_id} AND user_id={user_id}. The user_id is something you'll have saved in the session.
As a side note, when running database queries, you can bind the parameters, which means CI takes care of making the parameters safe. So you can amend your query above to be like this:
$this->db->query("DELETE FROM tasks WHERE task_id = ?", array($task_id));
You can read more on this here: http://codeigniter.com/user_guide/database/queries.html at the bottom (Query Bindings section).
As for your error of getting both an error and a success message, there might be a few different things going wrong. I'm not sure if something's gone wrong when copying the code, but you have an error in the PHP code:
$this->load->model('tasks_model','task_delete');
$this->task_delete($id);
Should be:
$id = $this->input->post('id'); // the data is posted, it won't be passed in as an argument to the controller function
$this->load->model('tasks_model','task_delete');
$this->task_delete->deleteTask($id);
And your query also seems to have a mistake, so it would be better to make it as I mentioned above.

Related

Trigger validation error in related model

I have a model in Yii that contains an array of another model type. I am then trying to validate that no duplicate emails are filled out in a form, where you can fill out for n number of persons at the same time.
My current approach is to trigger a custom validation of the "outer" model that holds all the entrants, however, that model is not accessible in the view, only the array of entrants is, and if I then trigger the error on the "outer" model, it will not be displayed to the user. Therefore I would like to trigger it for the first entrant that violates the rule, but how do I go about doing that?
My code that attempts this, looks like this so far:
/*
* Custom validation rule to hinder the same e-mail being used twice.
*/
public function noRepeatingEmails($attribute, $params)
{
if (!isset($attribute)) return;
$emails = array();
foreach($this->$attribute as $user)
{
if (isset($user) && strlen(trim($user->email)) != 0)
{
$emailToAdd = strtolower(trim($user->email));
if (in_array($emailToAdd, $emails))
{
$this->addError($user, '<my error message>');
return;
}
else
{
$emails[] = $emailToAdd;
}
}
}
}
This only results in a code 500 error though:
Illegal offset type
I presume that is because it is looking for the property "user" in my model, rather than adding an error to "$user" object.
How do I best accomplish this?
I have a .NET background, so I am probably doing loads wrong here however.
If I understood correctly from your comment, you want to validate your model before saving it. For this purpose, CActiveRecord provides beforeSave() method. You need to put this method inside your model:
protected function beforeSave()
{
if(parent::beforeSave())
{
if(/* Your validation goes here*/)
return true;
else
return false
}
else
return false;
}
When the result of this method is true, save() method will be called. Otherwise save() method won't be called and therefore no record will be saved into your database.

Exceeded maximum time error when overriding the newQuery on Laravel 4.0

So, I was trying to implement this answer for my other question on the same subject... and it keeps givin me the exceeded time error. Any clues?
This is on my product model. It inherits from Eloquent.
public function newQuery($excludeDeleted = true)
{
$user_permission = Auth::user()->permissions;
if( $user_permission->master )
return parent::newQuery();
else if( $user_permission->web_service )
{
$allowed_ids = array();
foreach( $user_permission->allowed_products()->get() as $allowed)
$allowed_ids[] = $allowed->id;
return parent::newQuery()->whereIn('id', $allowed_ids);
}
return parent::newQuery();
}
If the user is master there is no need to query scope on the request. But, if it isn't then I need to filter by the logged user's permissions.
UPDATE:
I tried the following code in a controller and it works alright:
$user_permission = Auth::user()->permissions;
echo "<PRE>"; print_r($user_permission->allowed_products()->get()); exit;
UPDATE 2:
Guys, I just found out that the problem was in this peace of code:
$allowed = Auth::user()->permissions()->first()->allowed_products()->get()->list('id');
It somehow give me an Maximum execution time of 30 seconds exceeded. If I put the exact same code in a controller, works like a charm, though! I also tried to put it in a scope, also worked. This it's really grinding my gears!
Elloquent has a function called newQuery. Controller does not. When you implement this function in a Model you are overriding the one in Elloquent. If you then invoke Elloquent methods that need a new query for your model before they can return, like ->allowed_products()->get(). Then you are calling your own newQuery() method recursively. Since the user permissions have not changed, this results in infinite recursion. The only outcome can be a timeout because it will keep on trying to determine a filtered product list which causes your newQuery() method to be called, which tries to determine the filtered product list before returning the query, and so on.
When you put the method into a Controller, it is not overriding the Elloquent newQuery method so there is no infinite recursion when trying to get the allowed_product list.
It would be more efficient to apply the filter to the product query based on whether the id is in the user's allowed_products() list using ->whereExists() and build up the same query as allowed_products() except now add condition that id from the query you are filtering is equal to the product id in the allowed products query. That way the filtering is done in the database instead of PHP and all is done in the same query so there is no recursion.
I don't see how your update code works. Illuminate\Database\Eloquent\Collection does not have any magic methods to call the relation functions, you should get an undefined method error trying to do that.
Can you try something like
public function newQuery($excludeDeleted = true)
{
// Returns `Illuminate\Database\Eloquent\Collection`
$user_permission = Auth::user()->permissions;
if ($user_permission->master)
{
return parent::newQuery();
}
else if ($user_permission->web_service)
{
// If here you was to call $user_permission->allowed_products()->get() not much is going to happen, besides probably getting an undefined method error.
$allowed_ids = Auth::user()->permissions()->allowed_products()->get()->lists('id');
return parent::newQuery()->whereIn('id', $allowed_ids);
}
return parent::newQuery();
}
Update: as per comments below I believe the problem is due to newQuery() being called multiple times as the code works just fine when called once in a controller. When this is applied to every query there is no need to collect all the IDs over and over again (assuming they're not going to change each time you call for them). Something such as the below will allow you to store these and only process them once per request rather than every time a query is run.
private $allowed_ids_cache = null;
public function newQuery($excludeDeleted = true)
{
$user_permission = Auth::user()->permissions;
if ($user_permission->master)
{
return parent::newQuery();
}
else if ($user_permission->web_service)
{
if ($this->allowed_ids_cache === null)
{
$this->allowed_ids_cache = Auth::user()->permissions()->allowed_products()->get()->lists('id');
}
return parent::newQuery()->whereIn('id', $this->allowed_ids_cache);
}
return parent::newQuery();
}

Laravel, update all relations when deleting a record

I have two models, Position and User. They have a One to many relation between them.
When I delete a position, I want all the related users to be detached from that position and attached to a different one (found by id).
I'm sure it's simple enough, but I've tried doing it in a foreach loop, without success:
public function postDelete($position)
{
$positionMembers = $position->users()->get();
foreach ($positionMembers as $member) {
$member->position_id = '4';
// fixed copy/paste var name error
$member->save()
}
// Was the position deleted?
if($position->delete()) {
// Redirect to the position management page
return Redirect::to('admin/positions')->with('success', Lang::get('admin/positions/messages.delete.success'));
}
// There was a problem deleting the position
return Redirect::to('admin/positions')->with('error', Lang::get('admin/positions/messages.delete.error'));
}
I've also tried:
$member->position()->associate($this->position->find(4));
but it doesn't work either. The position_id field always remains unchanged. Is there a more recommended way?
First off define without success, because it says nothing, and the code you're showing should work.
Anyway, I would suggest different approach, for using Eloquent save in a loop isn't the best way:
public function postDelete($position)
{
DB::transaction(function () use ($position, &$deleted) {
// run single query for update
$position->users()->update(['position_id' => 4]);
// run another query for delete
$deleted = $position->delete();
});
// Was the position deleted?
if($deleted) {
// Redirect to the position management page
return Redirect::to('admin/positions')->with('success', Lang::get('admin/positions/messages.delete.success'));
}
// There was a problem deleting the position
return Redirect::to('admin/positions')->with('error', Lang::get('admin/positions/messages.delete.error'));
}
With this, you make sure users don't get updated if there's some error(exception thrown) when deleting position and you execute 2 queries, no matter how many users there are to update.

How do i call the function I created in my Model on the view

I just created this function in the model to see who im following in my social network... how do i call it in the view??
function isfollowing($following){
$user_id = $this->session->userdata('uid');
$this->db->select('*');
$this->db->from('membership');
$this->db->join('following', "membership.id = following.tofollow_id");
$this->db->where("tofollow_id","$following");
$this->db->where("user_id", "$user_id");
$q = $this->db->get();
if($q->num_rows() > 0) {
return "yes";
} else {
return "no";
}
}
Now in my VIEW how do i call it being that i had already made a function to get the current logged on user's id and that is equal to $r->id
How do i call it here?? what goes after the "==" in that if statement?
THE VIEW
<?php if ( $r->id == ): ?>
It is not a good practice to call model function from view.
There are some alternatives about it. You can use anyone you like.
First
When you are loading a view call your model function and pass it in a variable
than this variable will be passed to view.
Controller
$following_status = $this->my_model->isfollowing($following);
$data['following_status'] = $following_status;
$this->load->view('my_view',$data);
View
<p>$following_status</p>
Secound
If you want to be independent of model you can create helper which you can
use anywhere in the application. You will have to create a CI instance to
get it working.
custom_helper.php
function isfollowing($following)
{
$CI = get_instance();
$user_id = $CI->session->userdata('uid');
$CI->db->select('*');
$CI->db->from('membership');
$CI->db->join('following', "membership.id = following.tofollow_id");
$CI->db->where("tofollow_id","$following");
$CI->db->where("user_id", "$user_id");
$q = $CI->db->get();
if($q->num_rows() > 0) {
return "yes";
} else {
return "no";
}
}
View
//load the custom helper before using it (you can autoload of in autoload.php)
//or use common way $this->load->helper('custom');
<p>isfollowing($yourparameter)</p>
You do the following:
(1) Load your model in the controller that creates your page or auto load it
(2) In your view, type something like:
$this->The_custom_model->isfollowing($theinputvariable)
where The_custom_model is the model where you defined the isfollowing() function.
$theinputvariable is the appropriate argument value for your function. Keep in mind that you have specified an object as the argument to your function so you need to think about that.
this is an amended version to what raheel posted showing an if check - probably not necessary for your question, but to give you some things to think about...
// check to see if anything come back from the database?
if ( ! $data['following_status'] = $this->my_model->isfollowing($following) ) {
// nothing came back, jump to another method to deal with it
$this->noFollowers() ; }
// else we have a result, and its already set to data, so ready to go
else {
// do more here, call your view, etc
}
databases can go down even if the web page is working so its good to get in the habit of checking the results. the more error checks you can do in your controller and models, the cleaner your view files will be.
To access model into your view you first load it into autoload file like this
$autoload['model'] = array('model_name');
then in view you can get it by using this line of code
$this->model_name->isfollowing($following)
in isfollowing you will pass your tofollow_id

Codeigniter tutorial delete Post doesn't work

After completing the tutorial from the codeigniter user guide I ran into a problem I was forcing for the last two hours. I am trying to add functionality to delete a post, selected by ID, I am new to PHP and couldn't find any solution for my problem:
The Controller
public function delete($id){
$id = $this->uri->segment(3);
$data['title'] = 'Delete an item';
if($this->news_model->delete_news($id)){
$this->load->view('templates/header', $data);
$this->load->view('news/success');
$this->load->view('templates/footer');
}else{
}
}
The Model
public function delete_news($id){
$this->db->where('id',$id)->delete('news');
return $this->db->affected_rows();
}
The Routing
$route['news/delete/(:num)'] = 'news/delete/$1';
I'm calling the function out of the index-page where all posts are shown with an anchor:
<p>Delete article</p>
and it calls the correct URL (http://localhost/webapp/index.php/news/delete/2) which should correctly execute and delete the post with the ID 2 from my news table.
I really can't understand where the mistake ism but by executing this, I get a 404.
What am I doing wrong?
In your function delete I don't see that you loaded the news_model. That could be the issue if it isn't auto-loading. Perhaps, start by verifying that the controller is talking to the model by inserting:
echo 'Hello Model';
in the delete_news function of your news_model.
EDIT:
Instead of
if($this->news_model->delete_news($id)){
//conditions
}
And
Have your model send a T/F based on it's execution. This will tell us if the error is in the SQL. By returning TRUE no matter what, we'll see if that model function even runs:
return TRUE;
Try to add the step (for error checking)
$del = $this->news_model->delete_news($id);
echo 'del';
if($del == TRUE){
//conditions
}
With the 404 - I'm also suspicious it's a routing issue. I'll take a look at that as well.

Categories