Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]Andy78[/eluser]
I am using the basic login manager that is in the example bugs application but my main admin controller now has about 25 functions in it and at the start of every function i need to call
Code:
$this->login_manager->check_login(1);

Is there a simple way of having this check done automatically in every function or will I have to go down the MY_Controller route and then inherit from that.


[eluser]WanWizard[/eluser]
Either use base controllers (the MY_Controller route), or use the _remap() method to capture all method calls, do your validation there, and then call the requested method.

[eluser]Andy78[/eluser]
Will use the base controller option..

Is there any good examples of datamapper being used to perform a search operation. I need to be able to add a filtering/search option to functions similar to the one below. I already have sort_by and sort order on the column headings working but want to be able to add a text box which will allow searching of specified columns on the table for key words and return the results.

I was thinking about adding a form to the view which will submit back to the same function in the controller and if the search form field is set add a like querys across the table columns to return the results. This sound like it will work?

Here is the controller function so far

Code:
//View the list of active Takeaways currently on the system
  
    function view_takeaways($page = 1, $sort_by = 'name', $sort_order = 'asc'){
        
        //check user is logged in and has admin rights
  $this->login_manager->check_login(1);
        
        $takeaways = new Takeaway();
        $takeaways->where('active =', 1);
        $takeaways->include_related('takeaway_type', array('type_name'), TRUE, TRUE);
        $takeaways->order_by($sort_by, $sort_order);
        
        //if there is no pagesize variable in the session set to 5
        if(!$this->session->userdata('pagesize'))
        {
            $page_size = 5;
            $this->session->set_userdata('pagesize', $page_size);
        }
        //if the post value is sent from the number of rows selectbox
        //make the paze size equal to that value
        if($this->input->post('sel'))
        {
            $page_size = $this->input->post('sel');
            $this->session->set_userdata('pagesize', $page_size);
        }
        
        $takeaways->get_paged_iterated($page, $this->session->userdata('pagesize'));
        
        //set the content for the view partial (this is the actual content of the page)
        $content_data['takeaways'] = $takeaways;
        $content_data['sort_order'] = $sort_order;
        $content_data['sort_by'] = $sort_by;
        
        $data['menu_active3'] = 'active';//Used with jquery admin menu
        $data['content'] = $this->load->view('admin/view_takeaways', $content_data, TRUE);
        $data['page_title'] = 'Get Stuffed | Admin Area | Takeaways';
    
        $this->load->view('admin/index', $data);
            
    }
#


[eluser]Andy78[/eluser]
The view:

The View:
Code:
<h2>List of Takeaways</h2>
&lt;?php
    $msg = $this->session->flashdata('success');
    $errors = $this->session->flashdata('errors');
    
    if(!empty($msg)){
        
        echo '<div class="ui-state-highlight ui-corner-all" span class="ui-icon ui-icon-alert">/span>
              <strong>Info:</strong> '.$msg.'</div>';
    }else if(!empty($errors)){
        
        echo '<div class="ui-state-error ui-corner-all" span class="ui-icon ui-icon-alert">/span>
              <strong>Alert:</strong> '.$errors.'</div>';    
    }
?&gt;
<div class="top_tbl">
    <div>
        &lt;?php echo 'Total number of records: '.$takeaways->paged->total_rows; ?&gt;
    </div>
    <div>
        &lt;form id="num_rows" name="num_rows" method="post" action="&lt;?= site_url('admin/view_takeaways')?&gt;"&gt;      
            <select name="sel" class=""  >
            <option value="">Number of rows</option>
            <option value="1">1</option>
            <option value="5">5</option>
            <option value="10">10</option>
            </select>
        &lt;/form&gt;
    </div>
</div>
<table class="admin_tbl">
    <thead>
        <th>&lt;?php echo anchor('admin/view_takeaways/'.$takeaways->paged->current_page.'/merchant_id/'.(($sort_order == 'asc' && $sort_by == 'merchant_id') ? 'desc' : 'asc'), 'ID');?&gt;</th>
        <th>&lt;?php echo anchor('admin/view_takeaways/'.$takeaways->paged->current_page.'/name/'.(($sort_order == 'asc' && $sort_by == 'name') ? 'desc' : 'asc'), 'Name');?&gt;</th>
        <th>&lt;?php echo anchor('admin/view_takeaways/'.$takeaways->paged->current_page.'/address/'.(($sort_order == 'asc' && $sort_by == 'address') ? 'desc' : 'asc'), 'Address');?&gt;</th>
        <th>&lt;?php echo anchor('admin/view_takeaways/'.$takeaways->paged->current_page.'/postcode/'.(($sort_order == 'asc' && $sort_by == 'postcode') ? 'desc' : 'asc'), 'Postcode');?&gt;</th>
        <th>&lt;?php echo anchor('admin/view_takeaways/'.$takeaways->paged->current_page.'/distance/'.(($sort_order == 'asc' && $sort_by == 'distance') ? 'desc' : 'asc'), 'Deliver distance');?&gt;</th>
        <th>&lt;?php echo anchor('admin/view_takeaways/'.$takeaways->paged->current_page.'/mile_cost/'.(($sort_order == 'asc' && $sort_by == 'mile_cost') ? 'desc' : 'asc'), 'Mile cost');?&gt;</th>
        <th>Email Address</th>
        <th>Phone Number</th>
        <th>Food Type</th>
        <th>Orders</th>
        <th>Edit</th>
        <th>Delete</th>
    </thead>
    <tbody>
&lt;?php
    foreach($takeaways as $takeaway){
        
        if($takeaway->pizza == 1){$pizza = 'Pizza ';}else{$pizza = '';}
        if($takeaway->chinese == 1){$chinese = 'Chinese ';}else{$chinese = '';}
        if($takeaway->indian == 1){$indian = 'Indian ';}else{$indian = '';}
        if($takeaway->kebab == 1){$kebab = 'Kebab ';}else{$kebab = '';}
?&gt;  
    <tr>
        <td>&lt;?php echo $takeaway->merchant_id;?&gt;</td>
        <td>&lt;?php echo $takeaway->name;?&gt;</td>
      &lt;!--<td>&lt;?php echo $takeaway->takeaway_type->type_name;?&gt;</td> --&gt;
        <td>&lt;?php echo $takeaway->address;?&gt;</td>
        <td>&lt;?php echo $takeaway->postcode;?&gt;</td>
        <td>&lt;?php echo $takeaway->distance;?&gt;</td>
        <td>&lt;?php echo $takeaway->mile_cost;?&gt;</td>
        <td>&lt;?php echo $takeaway->email;?&gt;</td>
        <td>&lt;?php echo $takeaway->phone;?&gt;</td>
        <td>&lt;?php echo $pizza.$chinese.$indian.$kebab;?&gt;</td>
        <td><a href="&lt;?php echo base_url();?&gt;index.php/admin/takeaway_orders/&lt;?php echo $takeaway-&gt;id;?&gt;">Orders</a></td>
        <td><a href="&lt;?php echo base_url();?&gt;index.php/admin/edit_takeaway/&lt;?php echo $takeaway-&gt;id;?&gt;">Edit</a></td>
        <td><a class="del_takeaway" href="&lt;?php echo base_url();?&gt;index.php/admin/delete_takeaway/&lt;?php echo $takeaway-&gt;id;?&gt;">Delete</a></td>
    </tr>    
&lt;?php    
}
?&gt;
    </tbody>
</table>
<div class="page_tbl">
<div>    
&lt;?php if($takeaways->paged->has_previous){
?&gt;    
    <td><a class="ui-corner-all ui-state-default" href="&lt;?= site_url('admin/view_takeaways/1') ?&gt;">&lt;&lt; First</a></td>
    <td><a class="ui-corner-all ui-state-default" href="&lt;?= site_url('admin/view_takeaways/'.$takeaways-&gt;paged-&gt;previous_page) ?&gt;">&lt; Prev</a></td>
&lt;?php
}
?&gt;
</div>
<div id="page-info">Page <strong>&lt;?= $takeaways->paged->current_page; ?&gt;</strong> / &lt;?= $takeaways->paged->total_pages; ?&gt;</div>
<div>
&lt;?php if($takeaways->paged->has_next){
?&gt;    
    <td><a class="ui-corner-all ui-state-default" href="&lt;?= site_url('admin/view_takeaways/'.$takeaways-&gt;paged-&gt;next_page) ?&gt;">Next &gt;</a></td>
    <td><a class="ui-corner-all ui-state-default" href="&lt;?= site_url('admin/view_takeaways/'.$takeaways-&gt;paged-&gt;total_pages) ?&gt;">Last &gt; &gt;</a></td>
&lt;?php
}
?&gt;
</div>
</div>

[eluser]DocO[/eluser]
[quote author="WanWizard" date="1334008371"]Don't you mean
Code:
if ($object->save()) {
instead of
Code:
if ($object->save) {
?

In development, ALWAYS enable all error reporting. You would have picked up the notice error informing you that $object->save does not exist.

And because it doesn't exist, it evaluates to false, causing the error to be printed all the time.[/quote]

You can't see the look of utter horror on my face right now...

I've been staring at this code so much, I couldn't see that when I moved stuff around, the parens dropped off. And someone had disabled debug on me Sad

[eluser]WanWizard[/eluser]
A form would work. Note that if you filter, the posted page number is no longer correct, so that that into account.

[eluser]WanWizard[/eluser]
[quote author="DocO" date="1334059801"]You can't see the look of utter horror on my face right now...

I've been staring at this code so much, I couldn't see that when I moved stuff around, the parens dropped off. And someone had disabled debug on me Sad
[/quote]
rofl Smile

[eluser]DocO[/eluser]
OK, now that my head is on straight, I've got validation apparently working, but it appears that overriding $error_prefix and $error_suffix does not work?

In my model, I've got:

Code:
&lt;?php

class Device extends DataMapper {


        var $error_prefix = '<div class="forminvalid">';
        var $error_suffix = '</div>';

        var $validation = array (
                'name' => array (
                        'label' => 'Name',
                        'rules' => array('required','xss_clean','unique'),
                ),
...

But here is the var_dump of the error object:

Code:
object(DM_Error_Object)#28 (3) {
  ["all"]=>
  array(1) {
    ["name"]=>
    string(34) "<p>The Name field is required.</p>"
  }
  ["string"]=>
  string(34) "<p>The Name field is required.</p>"
  ["name"]=>
  string(34) "<p>The Name field is required.</p>"
}

As you can see, the delimiters are still &lt;p> and &lt;/p>.

I can override it in the config/datamapper.php, but I don't know if I need to override on a case-by-case basis.

[eluser]WanWizard[/eluser]
Which version of Datamapper are you using?

In case it's 1.8.2 from the zip or from the spark, read this: http://ellislab.com/forums/viewthread/19...40/#994366

The current version on bitbucket, 1.8.2.1. has this issue fixed.

[eluser]DocO[/eluser]
[quote author="WanWizard" date="1334069983"]Which version of Datamapper are you using?

In case it's 1.8.2 from the zip or from the spark, read this: http://ellislab.com/forums/viewthread/19...40/#994366

The current version on bitbucket, 1.8.2.1. has this issue fixed.[/quote]

Yup, that was it. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB