We help IT Professionals succeed at work.

how is this saving without a table

jagguy
jagguy asked
on
In cakephp I have some code which saves data without a table and I have no idea how.
The model,controller show nothing like below it works fine but there is no tablecalled terms

 $terms=$this->Term->find('all' ); //displays everything and you can edit these fields in another page.

There is no table called terms in any database I have.

How is this happening?
<?php

App::uses('AppController', 'Controller');

/**
 * Subjects Controller
 *
 * @property Subject $Subject
 */
class TermsController extends AppController {


    public function add() {
        if ($this->request->is('post')) {
            $this->Term->create();
            if ($this->Term->save($this->request->data)) {
                $this->Session->setFlash(__('The Term has been saved'), 'flash_success');
                $this->redirect(array('controller' => 'configs', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Term could not be saved. Please, try again.'));
            }
        }
    }

Open in new window

Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Term is likely to be an instance of a class:

$this->Term = new someClass()

And that class will contain the methods you refer to - create(), save() etc.

Don't know how your app is coded, or the structure of it, but you could do a global search through your app for Term. When you find a line that is used to create the new object, then you'll know the name of the class that contains your methods. Those methods will tell you exactly what's going on.

If your app is based on a Model View Controller setup, then the class is likely to be a Model.

Author

Commented:
I cant find it.
Here is the Model and I cant see how this saves data

<?php
App::uses('AppModel', 'Model');
/**
 * Subject Model
 *
 * @property Lesson $Lesson
 * @property Student $Student
 * @property Tutor $Tutor
 */
class Term extends AppModel {

	var $displayField = "Title";

    public function get_current_term ( ) {   
        $today = date("Y-m-d");
        return $this->find('first',   array('conditions'=> array('AND' => array(array('Term.start_date <=' => $today), array('Term.end_date >=' => $today)))));;
    }
	
    public function get_neighbors_by_id ( $term_id ) {
        
        return $this->find('neighbors', array('field' => 'id', 'value' => $term_id));
    }
    
   public function get_term_by_id( $term_id ) {
       return $this->read(null, $term_id);
   }
}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
OK. Still need to dig deeper. In object oriented code you have the concept of inheritance. Basically (very) this means you can create a class that does 'certain things'. You can then extend that class to add extra things. In your case, the Term model inherits from the AppModel, so you need to look at the AppModel. All the methods from AppModel will be available to the Term model, so you'll probably find the create(), save() methods in there.

If you look at the AppModel class and can't find those methods, then see if the AppModel extends another class - this is fundamental Object Oriented coding.

Author

Commented:
Nope again.
Nothing here, nothing in app controller

App::uses('Model', 'Model');
App::uses('CakeEmail', 'Network/Email');

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */
class AppModel extends Model {

	var $actsAs = array('Containable');

    public $weekDays = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );

   public $dayTimes = array(
        '00:00',
        '08:00', '08:15', '08:30', '08:45',
        '09:00', '09:15', '09:30', '09:45',
        '10:00', '10:15', '10:30', '10:45',
        '11:00', '11:15', '11:30', '11:45',
        '12:00', '12:15', '12:30', '12:45',
        '13:00', '13:15', '13:30', '13:45',
        '14:00', '14:15', '14:30', '14:45',
        '15:00', '15:15', '15:30', '15:45',
        '16:00', '16:15', '16:30', '16:45',
        '17:00', '17:15', '17:30', '17:45',
        '18:00', '18:15', '18:30', '18:45',
        '19:00', '19:15', '19:30', '19:45',
        '20:00', '20:15', '20:30', '20:45',
    );

    public function wrapStr ($str, $q = "'") {
        return $q . $str . $q;
    }

    public function wrapValues ($arr, $q = "'") {
        foreach ($arr as $key => &$value) {
            $value = $q . $value . $q;
        }
        return $arr;
    }
	
	
	
		var $inserted_ids = array();

		function afterSave($created) {
			if($created) {
				$this->inserted_ids[] = $this->getInsertID();
			}
			return true;
		}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
As you can see, AppModel extends Model, so have a look there. You may need to keep digging deeper until you find what you need.

Author

Commented:
There is no model class in app, there is in lib folder but nothing to do with Term.

So where to as I really cant find this.

Author

Commented:
There is a term model yes and controller but where does it save to as nothing in the DB? There is no term table

<?php

App::uses('AppController', 'Controller');

/**
 * Subjects Controller
 *
 * @property Subject $Subject
 */
class TermsController extends AppController {


    public function add() {
        if ($this->request->is('post')) {
            $this->Term->create();
            if ($this->Term->save($this->request->data)) {
                $this->Session->setFlash(__('The Term has been saved'), 'flash_success');
                $this->redirect(array('controller' => 'configs', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Term could not be saved. Please, try again.'));
            }
        }
    }

    public function delete($id = null) {
        $this->Term->id = $id;
        if (!$this->Term->exists()) {
            throw new NotFoundException(__('Invalid Term'));
        }
        if ($this->Term->delete()) {
            $this->Session->setFlash(__('Term deleted'), 'flash_success');
            $this->redirect(array('controller' => 'configs', 'action' => 'index'));
        }
        $this->Session->setFlash(__('Term was not deleted'));
        $this->redirect(array('controller' => 'configs', 'action' => 'index'));
    }


    public function edit($id = null) {
        $this->Term->id = $id;
        if (!$this->Term->exists()) {
            throw new NotFoundException(__('Invalid Term'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Term->save($this->request->data)) {
                $this->Session->setFlash(__('The Term has been saved'), 'flash_success');
                $this->redirect(array('controller' => 'configs', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Term could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Term->read(null, $id);
        }
    }
}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
OK. What framework are you using?

Somewhere in your code, you'll create the object $this->Term. It will be created from a class (which may inherit from other classes). Without knowing the structure of your app, it's difficult to know precisely where to look.

It doesn't matter that there's not a Term table in yoru database. That's just the name of your object and doesn't have to match a DB Table.

Author

Commented:
Cakephp I am using and i never heard of saving data without a table .

You dont create an object $this->Term as this is cakephp
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
CakePHP is built on Objects, and does a lot of the object initialisation automatically, based on the names of your controllers, models and tables. A model represents a database table, and is automatically initialised from within a controller.

So a controller called TermsController will automatically initialise the Term model, which in turn will represent the 'terms' table from your database. All of this is done for you as long as you have used the proper naming conventions.

As for saving data without a table - you can save data anywhere - a text file for example - not all data belongs in a table!

Author

Commented:
ok so where is data being saved as I cant find anything
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Check your database.

Only you know how your app is configured, so you must have set it up to store data somewhere - if it's not a database, then it'll be some other data store - text files for example. The default location for a Term class is the Terms table in your database - if that's not the case you'll just have to dig around in your application - we can't possibly know how you've configured it.

If you didn't write this app, then I would strongly suggest you start reading up on the basics of Object Oriented Programming and CakePHP in particular.

Author

Commented:
In other words you dont know where to look like me because nothing you have said I havent tried
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
I've advised you look in all the standard places, but it seems like your app is not configured in a standard way so I'm all out of ideas.

As I said previously, by default, the TermsController will initialise the Term model, which represents the Terms table in your database. If this is not the case in your app, then it's likely to be configured in a non-standard way, and unfortunately I can't guess at what you've done, and you seem to not know what you've done, which is understandable if you didn't build the app.