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;
}
}
}
Related
I try to fetch data from database in codeigniter construct function
but getting undefined method error
controller-
class Welcome extends CI_Controller
function __construct()
{
parent::__construct();
$this->load->model('Login');
$this->Login->getadminnav();
}
}
model-
class Login extends CI_Model
{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}
You got to load the model you are using first:
$this->load->model('login_model');
$this->Login->getadminnav();
And remember to rename the model to Login_model
Try these pieces of code.
Controller:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login');
$this->Login->adminnav();
}
}
Model:
class Login extends CI_Model{
public function adminnav(){
$this->load->database();
$sql = "SELECT * from adminnav where status='1'";
$result = $this->db->query($sql);
return $result;
}
}
I am seeing in your code that-
In controller you didn't start the second bracket after controller name.
You defined the function name as "adminnav" in model but called it as "getadminnav" in Controller
You didn't load the database in model (If you already loaded it in autoload.php then here is not needed)
Anyway please let me know whether your problem is solved or not.
you are calling getadminnav() but in your model the method name is adminnav.and change model name Login to Login_model.
try this:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login_model');
$this->Login->adminnav();
}
}
// model
class Login_model extends CI_Model{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}
I have 2 simple models:
class Page extends ActiveRecord {
public static function tableName() {
return 'page';
}
public function getPageType() {
return $this->hasOne(PageType::className(), ['id', 'page_type_id']);
}
}
class PageType extends ActiveRecord {
public static function tableName() {
return 'page_type';
}
public function getPages() {
return $this->hasMany(Page::className(), ['page_type_id', 'id']);
}
}
I am trying to get a list of pages and filter them by page_type_id, so i am doing:
Page::find()
->joinWith('pageType')
->where(['page.service_id' => $this->serviceId, 'page.active' => 1])
->all();
But I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'page_type.0' in 'on clause'
The SQL being executed was: SELECT `page`.* FROM `page`
LEFT JOIN `page_type`
ON
`page`.`id` = `page_type`.`0`
AND
`page`.`page_type_id` = `page_type`.`1`
WHERE
(`page`.`service_id`=1) AND (`page`.`active`=1)
Any ideas?
You need to provide the column names in the relation as name=>value pairs, not comma-separated list.
return $this->hasOne(PageType::className(), ['id', 'page_type_id']);
and
return $this->hasMany(Page::className(), ['page_type_id', 'id']);
Change your relations and your classes code look like following
class Page extends ActiveRecord {
public static function tableName() {
return 'page';
}
public function getPageType() {
return $this->hasOne(PageType::className(), ['id'=> 'page_type_id']);
}
}
class PageType extends ActiveRecord {
public static function tableName() {
return 'page_type';
}
public function getPages() {
return $this->hasMany(Page::className(), ['page_type_id'=>'id']);
}
}
I imagine I might need to add a special annotation somewhere, but I'm crossing my fingers that PhpStorm is smart enough to resolve return types given awkward inheritance patterns.
For example, I have some code that resembles this:
<?php
class Collection extends \ArrayObject
{
public function __construct(array $items)
{
foreach ($items as $key => $value) {
if (isset(static::$requiredType) && !$item instanceof static::$requiredType)
$this->offsetSet($key, $value);
}
}
public function getFirst()
{
return $this->offsetGet(0);
}
}
class MessageCollection extends Collection
{
protected static $requiredType = 'Message';
}
class UserCollection extends Collection
{
protected static $requiredType = 'User';
}
I'd like it if when I call UserCollection::getFirst() it inferred that a User was returned, while when I call MessageCollection::getFirst() it inferred that a Message was returned. Is there some annotation I could put in somewhere to achieve this result?
My first thought was something like this:
/**
* #return Message|User|XXXX|YYYY|ZZZZ|AAAA|BBBB|CCCC|DDDD
*/
public function getFirst()
{
return $this->offsetGet(0);
}
but I imagine that would get a little ridiculous to the point of being useless as I add more collection classes.
Try this:
/**
* #method \User getFirst()
*/
class UserCollection extends Collection
{
protected static $requiredType = 'User';
}
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);
}
}
}
I need to increment an integer variable in a function within the document class upon transpiring event in another class. I can't seem to figure out how to call the function and reference the variable.
as3 newbie, please help!
The proper scope needs to be in place and the proper packaging.
Declare a static variable to handle your access to the Main Document Class
private static var _instance:Main;
public static function get instance():Main { return _instance; }
public function Main() { // constructor
_instance = this;
}
Declare some getters and setters in the Main Document Class
private var _foo:int = 0;
public function get foo():int{
return _foo;
}
public function set foo(value:int):void {
_foo= value;
}
And then in any class you need you can change to something as follows,
public class O {
public function O() {
Main.instance.set(Main.instance.get() + 1);
}
}
simple example, defining a variable 'test' in the document class:
package {
public class DocumentClass extends Sprite {
public static var test:Number = 3;
public function DocumentClass() {
test = 4;
}
}
}
now access the 'test' variable in another class:
package {
public class OtherClass extends Sprite {
public function OtherClass() {
DocumentClass.test = 5;
}
}
}
does this apply to your code?