I have this in all my Controllers for Menu, but I want to give it once in the core folder of CI Any Suggestions?
> $data['cms_menus']=$this->database_model->GetRecords('cms_menus',
> false, array('FKMenuID'=>null));
> foreach ($data['cms_menus'] as $key => $datas){
> $data['cms_menus'][$key]['childs']=$this->database_model->GetRecords('cms_menus',
> false, array('FKMenuID'=> $datas['PKMenuID']));
One way is to extend CI_Controller
class MY_Contoller extends CI_Controller
{
public $data;
public function __construct()
{
parent :: __construct();
$this->load->model('database_model');
$data['cms_menus']=$this->database_model->GetRecords('cms_menus', false, array('FKMenuID'=>null));
foreach ($data['cms_menus'] as $key => $datas)
{
$this->data['cms_menus'][$key]['childs']=$this->database_model->GetRecords('cms_menus', false, array('FKMenuID'=> $datas['PKMenuID']));
}
}
Use MY_Controller to define the rest of your controllers.
class Other_contoller extends MY_Controller
{
public function __construct()
{
parent :: __construct();
//other construct code here
}
public function index()
{
//use the menu in a view
$this->load->view('some_view', $this->data);
//The view can use $cms_menus as it is in $this->data
}
}
Related
How do I handle multiple inverse relations pointing to the same active record?
For example:
class Bicycle extends ActiveRecord {
public function getFrontWheel() {
return $this
->hasOne(Wheel::class, ['id' => 'front_wheel_id'])
->inverseOf('bicycles');
}
public function getRearWheel() {
return $this
->hasOne(Wheel::class, ['id' => 'rear_wheel_id'])
->inverseOf('bicycles');
}
}
class Wheel extends ActiveRecord {
public function getBicycles() {
return $this
->hasMany(Bicycle::class, ['???' => 'id'])
->inverseOf('??????');
}
}
What can I do here? I critically need the inverse relations.
Here is my own solution.
Key points:
It all boils down to proper naming.
Inverse relations are bijective! In other words, every relation always has to have its own unique mirror relation on the other end.
class Bicycle extends ActiveRecord {
public function getFrontWheel() {
return $this
->hasOne(Wheel::class, ['id' => 'front_wheel_id'])
->inverseOf('frontWheelBicycles');
}
public function getRearWheel() {
return $this
->hasOne(Wheel::class, ['id' => 'rear_wheel_id'])
->inverseOf('rearWheelBicycles');
}
}
class Wheel extends ActiveRecord {
public function getFrontWheelBicycles() {
return $this
->hasMany(Bicycle::class, ['front_wheel_id' => 'id'])
->inverseOf('frontWheel');
}
public function getRearWheelBicycles() {
return $this
->hasMany(Bicycle::class, ['rear_wheel_id' => 'id'])
->inverseOf('rearWheel');
}
}
i would suggest to do the following:
create two new classes:
class FrontWheel extends Wheel {
class RearWheel extends Wheel {
in new classes you can set easily the relation.
How to instantiate the correct class? There is a method in ActiveRecord instantiate() where you can write your logic which wheel class need to be created.
class Wheel extends ActiveRecord {
...
public static function instantiate ( $row ) {
if($row['type'] === 'RearWheel') {
return new RealWheel();
}
...
}
full code:
class Bicycle extends ActiveRecord
{
public function getFrontWheel()
{
return $this
->hasOne(Wheel::class, ['id' => 'front_wheel_id'])
->inverseOf('bicycles');
}
public function getRearWheel()
{
return $this
->hasOne(Wheel::class, ['id' => 'rear_wheel_id'])
->inverseOf('bicycles');
}
}
abstract class Wheel extends ActiveRecord
{
public static function instantiate($row)
{
if ($row['type'] === 'RearWheel') {
return new RealWheel();
}
if ($row['type'] === 'FrontWheel') {
return new FrontWheel();
}
throw new InvalidConfigException();
}
abstract public function getBicycles();
}
class RealWheel extends Wheel
{
public function getBicycles()
{
return $this
->hasMany(Bicycle::class, ['rear_wheel_id' => 'id'])
->inverseOf('rearWheel');
}
}
class FrontWheel extends Wheel
{
public function getBicycles()
{
return $this
->hasMany(Bicycle::class, ['front_wheel_id' => 'id'])
->inverseOf('frontWheel');
}
}
I'm trying to use the findOn from within the class that I want to search. Is this possible or is there a better way?
class CmsSettings extends ActiveRecord
{
public static function tableName()
{
return 'cms_settings';
}
//not working
public static function run(){
$results = CmsSettings::findOn(1):
return $results;
}
// not working
public static function check(){
$results = CmsSettings::findOn(1):
if($results->somesetting){
return true;
}
}
}
You should probably use static::findOne(1). By using self or CmsSettings you are just hardcoding returned type, which makes this class less flexible and will give you unexpected results on inheritance. For example if you create child model which extends your class:
class CmsSettings extends ActiveRecord {
public static function run() {
$results = CmsSettings::findOne(1);
return $results;
}
// ...
}
class ChildCmsSettings extends CmsSettings {
}
You expect that ChildCmsSettings::run() will return instance of ChildCmsSettings. Wrong - you will get CmsSettings. But if you write this method with using static:
public static function run() {
$results = static::findOne(1);
return $results;
}
You will get instance of class which you're used for call run() - ChildCmsSettings.
Use self
Refer findOne()
class CmsSettings extends ActiveRecord
{
public static function tableName()
{
return 'cms_settings';
}
public static function run()
{
$results = self::findOne(1);
return $results;
}
public static function check()
{
$results = self::findOne(1);
if ($results->somesetting) {
return true;
}
}
}
I have implemented the below code for ssl implementation, but after that my redirect is no longer working:
Here is my code:
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
class WidgetsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Security', ['blackHoleCallback' => 'forceSSL']);
}
public function beforeFilter(Event $event)
{
$this->Security->requireSecure();
}
public function testRedirect()
{
**$this->redirect("/controller/Somewhere");**
}
public function forceSSL()
{
return $this->redirect('https://' . env('SERVER_NAME') . $this->request->getRequestTarget());
}
}
Any help is appreciated.
I don`t understand how savind data from model (like User.php)
When i run saveData() from Controller new row not creared.
class Users extends \yii\db\ActiveRecord
{
....
public function saveData() {
$this->name = 'test_user';
$this->save();
}
}
I don`t want save data from controller.
What i do wrong?
Check if there are validation errors:
class Users extends \yii\db\ActiveRecord
{
....
public function saveData() {
$this->name = 'test_user';
if($this->save() == false)
{
var_dump($this->errors);
}
}
}
My model: as seen below, very basic
class User extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getAll()
{
$this->db->order_by("lastName", "asc");
$this->db->order_by("firstName", "asc");
$this->db->order_by("userName", "asc");
$query = $this->db->get('user');
// test for result
if($query->num_rows() > 0)
{
return $query->result();
}
return NULL;
}
}
My controller: actually part of my controller, every time loading the users/display function by default route, the error (further down) shows up. Should a model loaded in a controller's contructor be available for all other function in the same controller?
class Users extends CI_Controller
{
function __contruct()
{
parent::__construct();
$this->load->model('user');
}
function display()
{
$data['users'] = $this->user->getAll();
$head['pageTitle'] = 'Users Panel';
$this->load->view('security/redirect');
$this->load->view('template/head', $head);
$this->load->view('user/usersPanel', $data);
$this->load->view('template/foot');
}
}
My error: refering to the line, "$data['users'] = $this->user->getAll()", in above controller
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$user
My environment:
Codeigniter 2.1.0;
Mac Lion;
MAMP 2.0;
Shouldn't this:
class Users extends CI_Controller
{
function __contruct()
{
be like this:
class Users extends CI_Controller
{
function __construct()
{
replace contruct with construct.
Shouldn't this:
$data['users'] = $this->user->getAll();
be this:
$data['users'] = $this->user_model->getAll();
sorry
also the model name:
$this->load->model('user_model');
and class name User_model extends CI_Model
All of my CI projects are set up this way.
http://codeigniter.com/user_guide/general/models.html