Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    •  
      CommentAuthorbigtalk
     
    Hello,

    I am attempting to create an admin controller with 2 models, News and Track. Both models are independent of each other but do similar things.
    I created the controller and the models. I then scaffolded News. Everything looks great. So, how do I incorporate my Track model?
    It seems that it would make sense to reuse most of the code from News (since Track does some of the same types of things.)
    What it should do is:
    We click on the link "Create new News" in admin/listing and we add new News.
    We click on the link "Add new Track" in admin/listing and we add a new Track.

    I've tried many different things and am finally asking for help. I am a real noob in PHP and Akelos, but I love it! Also, my hosting company is only running PHP4.

    Thank you in advance for any input on my issue. - BigTalk
    • CommentAuthorkkrzyzak
     

    hi. First at all, try to create admin module, not controller: http://wiki.akelos.org/modules However, I've recently had simmilar problem. I had permission controller, and two models: permission and permission_scheme. Both of theme are scaffolded. Sollution: ` class Admin_PermissionController extends AdminController { var $models = 'permission,permission_scheme';

           function listing(){
           //standard, like in normal scaffold (you can use scaffold generator, then copy->paste)
           }
            //other functions
           function listingScheme(){
           //generate scaffold for permission_scheme, then copy->paste here
            }
    

    ` then, all You need is edit a little bit views (for example form actions) I can click "Create permission" in permission/listing and add permission (render add() function) I can click "Create permission scheme" in permission/lisitng and add permission scheme (render addScheme() function) Sorry for my language errors - English isn't my native language.

    •  
      CommentAuthorbigtalk
     

    Thank you for your help!
    I did as you said (mostly.) I couldn't get a module to work right. I was able to make two separate models work. The problem really is that it doesn't utilize D.R.Y. (Don't Repeat Yourself). I end up with code that looks like this in my Admin Controller:

    <?php
    
    class AdminController extends ApplicationController
    {
    var $models = array('track', 'news');
    
        function index()
        {
            $this->renderAction('control');
        }
    
        function control()
        {
          /** control.tpl has links to both editNews and editTrack and they work! **/
        }
    
        /** edit for news model **/
        function editNews()
        {
            if(!empty($this->params['id'])){
                if(empty($this->news->id) || $this->news->id != $this->params['id']){
                    $this->news =& $this->News->find($this->params['id']);
                }
            }else{
                $this->redirectToAction('previewNews');
            }
    
            if(!empty($this->params['news'])){
                $this->news->setAttributes($this->params['news']);
                if($this->Request->isPost() && $this->news->save()){
                    $this->flash['notice'] = $this->t('News was successfully updated.');
                    $this->redirectTo(array('action' => 'showNews', 'id' => $this->news->getId()));
                }
            }
        }
    
        /** edit for track model **/
        function editTrack()
        {
            if(!empty($this->params['id'])){
                if(empty($this->track->id) || $this->track->id != $this->params['id']){
                    $this->track =& $this->Track->find($this->params['id']);
                }
            }else{
                $this->redirectToAction('previewTrack');
            }
    
            if(!empty($this->params['track'])){
                $this->track->setAttributes($this->params['track']);
                if($this->Request->isPost() && $this->track->save()){
                    $this->flash['notice'] = $this->t('This Track was successfully updated.');
                    $this->redirectTo(array('action' => 'showTrack', 'id' => $this->track->getId()));
                }
            }
        }    
    

    Sorry for the big code block.

    editNews and editTrack both are essentially the same code, just different models. Shouldn't there be a way to have an action called edit that both editNews and editTrack refer to?
    I am pretty new to Akelos and PHP in general. Perhaps I'm off base and should just be happy that the code I have works.
    Input is so appreciated.

    • CommentAuthorkkrzyzak
     

    Thank you for your help! I'm glad, that I can help someone - not only always ask for help ;)

    I think that Your way of implemention isn't the best: Try to create admin module, where You can check, that user has permission, etc. Then, create two controllers - news and track. I mean, create something like that: admin_controller.php

    `

         class AdminController extends ApplicationController 
           {
    public function __construct(){
                  $this->beforeFilter(array('_authenticate'=>array('except'=>array('authenticate'))));
             }
    
           function _authenticate(){
    
            /*here put code,where You can check user permissions */
           }
    

    } `

    then, create admin folder in app/controllers. In this folder, create news_controller.php: class Admin_NewsController extends AdminController { /*listing() edit() etc. */ } the same for track. then edit config/routes.php, and add as first: $Map->connect('/admin/:controller/:action/:id', array('controller' => 'dashboard', 'action' => 'index', 'module' => 'admin'));

    Sorry for my language errors - English isn't my native language.

    •  
      CommentAuthorbigtalk
     

    Your English is fine!

    I thought of making News and Track their own controllers, but it seemed like I'd be repeating the same code over and over...
    You're probably right though. Copy and paste works great too.

    Thanks for taking some time to help!

    • CommentAuthorkkrzyzak
     

    I thought of making News and Track their own controllers, but it seemed like I'd be repeating the same code over and over... It's not the same code- it's logically different. You'll like it - with separate controlers, You can easly edit code. You're probably right though. Copy and paste works great too. copy paste? generators are much better ;)

    •  
      CommentAuthorbigtalk
     

    Again kkrzyzak, thank you for your help!

    I did make a couple new controllers and it all works great! I came across this post:
    consolidating views: controller and table as variable?
    This person is looking at it in a way similar to how I was.
    Anyways, I'm up and running. These are thoughts for the next project. Cheers!