Trying to use the timehelper $this->Time->wasWithinLast($how_often, $last_updated);
But I keep getting
Error: Call to a member function wasWithinLast() on a non-object
It seems like it can't find $this->Time? Is that correct?
$how_often and $last_updated are both in the correct format.
SOLUTION:
Here is the solution. $this->Time only works in view. Here is how it works in model:
CakeTime::wasWithinLast($how_often, $last_updated);
This is the beginning of my reminders controller:
class RemindersController extends AppController {
/**
* Components
*
* #var array
*/
public $components = array('Paginator');
/**
* index method
*
* #return void
*/
public function index() {
var_dump($this->Time);
$this->Reminder->recursive = 0;
$this->set('reminders', $this->Paginator->paginate());
}
Here is the model:
class Reminder extends AppModel {
public function beforeSave($options = array())
{
// Attribute to this user
$this->data['Reminder']['user_id'] = AuthComponent::user('id');
$this->data['Reminder']['how_often'] = $this->data['Reminder']['number'].' '.$this->data['Reminder']['frame'];
$this->data['Reminder']['last_reminded'] = $this->data['Reminder']['created'];
}
public $virtualFields = array(
'remindable' => 'Reminder.created'
);
public function afterFind($results, $primary = false){
parent::afterFind($results, $primary);
foreach ($results as $key => $val) {
$results[$key]['Reminder']['remindable'] = $this->remindable($results[$key]['Reminder']['how_often'], $results[$key]['Reminder']['last_reminded']);
// $results[$key]['Reminder']['remindable'] = $this->Time->wasWithinLast($results[$key]['Reminder']['how_often'], $results[$key]['Reminder']['last_reminded']);
// $results[$key]['Comments']
}
// $results = Set::sort($results, '{n}.Item.score', 'desc');
return $results;
}
From the doc:
http://book.cakephp.org/3.0/en/core-libraries/time.html#namespace-Cake\I18n
"If you need TimeHelper functionalities outside of a View..."
$time = new Time('2014-06-15'); // your custom date here
$time->wasWithinLast($how_often, $last_updated);
http://book.cakephp.org/3.0/en/core-libraries/time.html#comparing-with-intervals
Related
I'm trying to create a class function which resembles how we used to fetch database listing and convert into a dropdown listing.
eg: DB::table()->where()->get()
what i would like to achieve in laravel custom class or through model is this
Dropdown::fetch()->toArray()
Dropdown::fetch()->toDropdown()
I tried to figure out how this can be done through google. But couldn't find any solution to it.
I'm using laravel 5.8
--
Edit - Sample Code added
Code tried:
namespace App\Http\Models;
use DB;
use Closure;
use BadMethodCallException;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Eloquent\Model;
class Dropdown extends Model
{
private $result = [];
private $default;
public function _cities(){
$tbl_cities = config("tables.TBL_meta_cities");
$result = DB::table($tbl_cities)->select('id', 'cityname')
->orderBy('id')->get()->toArray();
$this->result = $result;
}
public function _select(){
}
public function _list(){
return $this->result;
}
public function _setDefault($def=''){
}
public static function __callStatic($method, $parameters)
{
$action = '_'.$method;
if(method_exists(get_called_class(), $action))
self::$action(...$parameters);
else echo 'not found';
}
public function __call($method, $parameters)
{
$action = '_'.$method;
if(method_exists($get_called_class(), $action))
self::$action(...$parameters);
else echo 'not found';
}
}
and i tried
Dropdown::cities()->list()
but ended with bugs
Well i figured it out myself.
class Dropdown extends Model
{
private static $result = [];
private function getCities(){
$result = City::select('id', 'cityname')
->orderBy('id')->get()->toArray();
self::$result = $result;
}
public function toArray(){
return self::$result;
}
public function toDropdown(){
// Do the dropdown works
}
/**
* Dynamically handle calls to the class.
*
* #param string $method
* #param array $parameters
* #return mixed
*
* #throws \BadMethodCallException
*/
public function __callMethod($method, $parameters){
// Check with inclusive
$class = get_called_class();
$avail = false;
$action = '';
// Check method availability - direct
if(!$avail){
$action = $method;
$avail = method_exists($class, $action);
}
// Check method 2
if(!$avail){
$action = 'get'.ucwords($method);
$avail = method_exists($class, $action);
}
if($avail){
// Call the method
$return = self::$action(...$parameters);
if(!empty($return)) return $return;
} else {
// Throw error if method not found
throw new BadMethodCallException("No such method exists: $name");
}
return new self;
}
public static function __callStatic($method, $parameters){
return (new self)->__callMethod($method, $parameters);
}
public function __call($method, $parameters){
return (new self)->__callMethod($method, $parameters);
}
}
All i need to do is return new self which does the trick instead of return $this so that the trailing function can be called easily.
Now i can able to call that function like this
Dropdown::cities()->toArray();
Reference:
https://stackoverflow.com/a/41631711/1156493
Thank you #Joseph for your time & support.
Please, could you help me a bit with my problem?
I have class called Translateable and then clasess Article and Banner, which extend this class.
Problem occurs, when I do this:
$article = (new Article)->find(15);
$banner = (new Banner)->find(1);
$articleTrans = $article->trans(); // method trans is method from Translateable
When I call $article->trans(); I expect output like this:
App\Models\ArticleTrans
Article
but it return this:
App\Models\ArticleTrans
Banner
First row is ok, but the second one if bad and I don't know, how to solve this problem.
I need to have $instance stored as static property.
Could you give me you help?
class Translateable extends Model {
static $transLang = null;
static $transClass = null;
static $instance = null;
public function __construct(array $attributes = array()) {
static::$transLang = App::getLocale();
parent::$transClass = static::$transClass;
parent::$instance = static::$instance;
parent::__construct($attributes);
}
/**
* get items trans
*
* #param null $lang
* #return mixed
*/
public function trans($lang = null) {
if($lang == null) {
$lang = static::$transLang;
}
echo static::$transClass;
echo class_basename(static::$instance);
die();
}
public static function find($primaryKeyVal, $columns = []) {
$tci = new static::$transClass;
$item = static::withTrans()->where(static::$instance->getTable() . '.' . static::$instance->primaryKey, '=', $primaryKeyVal)->where($tci->getTable() . '.lang', '=', static::$transLang)->first();
return $item;
}
}
class Article extends Translateable {
static $transClass = 'App\Models\ArticleTrans';
public function __construct(array $attributes = array()) {
parent::$transClass = static::$transClass;
parent::$instance = $this;
parent::__construct($attributes);
}
}
class Banner extends Translateable {
static $transClass = 'App\Models\BannerTrans';
public function __construct(array $attributes = array()) {
parent::$transClass = static::$transClass;
parent::$instance = $this;
parent::__construct($attributes);
}
}
I'm solving quite strange problem which I'm facing for the first time.
I have some main Class with property static::$someProperty and I extend with this class other classes, e.g. ClassA and ClassB.
Problem is now, when I load
$classA = ClassA
and set there
static::$someProperty = "ClassA"
and echo this value, it works fine and return "ClassA" but then I also load
$classB = ClassB
and set
static::$someProperty = "ClassB"
and when I
echo static::$someProperty
in $classA now, there is value "ClassB".
Do you know, how to solve this problem? Probably it is connected with static, but I don't now, what to do with this.
class Translateable extends Model{
public static $transLang;
public static $transClassInstance;
public static $instance;
public $transInstance = null;
public function __construct(array $attributes = array()) {
self::$transLang = App::getLocale();
$tcName = static::$instance->transClass;
static::$transClassInstance = new $tcName;
parent::__construct($attributes);
}
/**
* add trans to the item
*
* #return mixed
*/
public static function withTrans($lang = null) {
if($lang == null) {
$lang = static::$transLang;
}
return static::join(static::$transClassInstance->getTable(), function ($join) use ($lang) {
$join->on(static::$instance->getTable() . '.' . static::$instance->primaryKey, '=', static::$transClassInstance->getTable() . '.' . static::$instance->primaryKey)->where(static::$transClassInstance->getTable() . '.lang', '=', $lang);
})->where(static::$transClassInstance->getTable() . '.lang', '=', $lang)
;
}
}
class Nested extends Translateable{
// protected $lft, $lvl, $rgt, $parent_ID;
public static $transClassInstance;
public static $transLang;
public function __construct(array $attributes = array()) {
self::$transLang = App::getLocale();
$tcName = static::$instance->transClass;
static::$transClassInstance = new $tcName;
parent::$instance = $this;
parent::__construct($attributes);
}
/**
*
* get $this item child
*
* #return null
*/
public function getChilds() {
$primaryKeyName = $this->primaryKey;
$parent_id = $this->$primaryKeyName;
// here is echo PageTrans instead of ProductCategoryTrans
echo static::$transClassInstance->getTable().'<br/>';
echo static::$transClassInstance->getTable() . '.lang'.'<br/>';
$query = static::where('parent_ID', '=', $parent_id)->where(static::$transClassInstance->getTable() . '.lang', '=', static::$transLang);
echo $query->toSql();
$this->generateItemsQuery($query);
$query->orderBy('lft', 'ASC');
$categories = $query->get();
return $categories;
}
}
class ProductCategory extends Nested{
public $transClass = 'App\Models\ProductCategoryTrans';
public function __construct(array $attributes = array()) {
static::$instance = $this;
parent::__construct($attributes);
}
}
class Page extends Nested{
public $transClass = 'App\Models\PageTrans';
public function __construct(array $attributes = array()) {
static::$instance = $this;
parent::__construct($attributes);
}
}
Example usage:
// find product category with ID == 1
$productCategory = (new ProductCategory)->find(1); // "ClassA"
// get some page...
$page = (new Page)->find(1); // find page with ID == 1 // "ClassB"
// get childs of loaded category
$categoryChilds = $productCategory->getChilds(); // get this category
Try to use self in classA and classB
self::$someProperty = 'test';
I'm calling a class by a string variable passed on a function argument.
ApiTester.php
use MyApp\Sites\Site;
abstract class ApiTester extends TestCase() {
/**
* Make a new record in the DB
*
* #param $type
* #param array $fields
* #throws BadMethodCallException
*/
protected function make($type, array $fields = [])
{
while($this->times--)
{
$stub = array_merge($this->getStub(), $fields);
$type::create($stub);
}
}
SitesTester.php
class SitesTester extends ApiTester() {
/** #test */
public function it_fetches_a_single_site()
{
// arrange
$this->make('Site');
// act
$site = $this->getJson('api/v1/sites/1')->data;
// assertion
$this->assertResponseOk();
$this->assertObjectHasAttributes($site, 'name', 'address');
}
Site.php // Eloquent Model
namespace MyApp\Sites;
class Site extends \Eloquent {
}
But if I call the class that the string variable $type contains, for example; string variable $type contains 'Site', it says class 'Site' not found.
I tried to manually type Site::create($stub) and finally accepts it.
I also tried
call_user_func($type::create(), $stub);
and
$model = new $type;
$model->create($stub);
but unfortunately it says class 'Site' not found.
Any ideas?
You're almost there:
class X {
static function foo($arg) {
return 'hi ' . $arg;
}
};
$cls = 'X';
print call_user_func("$cls::foo", 'there');
If your php is very old (<5.3 I believe), you have to use an array instead:
print call_user_func(array($cls, "foo"), 'there');
You may want to replace that static class call with the following :
while( $this->times-- )
{
$stub = array_merge( $this->getStub(), $fields );
call_user_func( "$type::create", $stub );
}
Runnable code here : http://runnable.com/VIqy4CDePeY-AeMV/output
I have an action in my controller called createAction. I also have a model My_Application_Product, that I'm using to create the product within the system. I'm following the Architecting Your Models talk. Is this the "correct" way to save my product? Code for My_Application_Product follows below.
class ProductController extends Zend_Controller_Action {
public function createAction() {
$categoryAdapter = new Application_Model_Categories();
$categories = $categoryAdapter->fetchAll('parent_id IS NOT NULL');
$form = new My_Application_Forms_Product_Create();
$category = $form->getElement('category');
foreach ($categories as $cat) {
$category->addMultiOption($cat->id, $cat->name);
}
if ($this->getRequest()->isPost()) {
if (! $form->isValid($_POST)) {
$this->view->form = $form;
return $this->render('create');
}
$product = new My_Application_Product();
$product->name = $_POST['name'];
$product->company_id = 1;
$product->category_id = $_POST['category'];
$product->trade_names = $_POST['trade_names'];
$product->website = $_POST['website'];
$product->description = $_POST['description'];
$product->closed_loop = $_POST['closed_loop'];
$product->sold_as = $_POST['sold_as'];
$product->sold_in = $_POST['sold_in'];
$product->dilution = $_POST['dilution'];
$id = $product->save();
$url = $this->getHelper('Url')
->url(array('action' => 'registryservices', 'id' => $id));
$this->_redirect($url);
}
$this->view->form = $form;
}
}
'
class My_Application_Product implements My_Application_Product_Interface {
// declare all the internally used variables here.
// if something isn't showing up when trying to save, that's probably
// because it's missing from here
protected $_id;
protected $_name;
protected $_company_id;
protected $_trade_names;
protected $_website;
protected $_description;
protected $_closed_loop;
protected $_sold_as;
protected $_sold_in;
protected $_dilution;
protected $_category_id;
protected $_verification_level;
protected $_dfe_sccp;
protected $_dfe_siicp;
protected $_el_ccd_hsc;
public function __set($name, $value) {
$local_var_name = "_" . $name;
if (property_exists($this, $local_var_name)) {
$this->{$local_var_name} = $value;
}
}
public function __get($name) {
$local_var_name = "_" . $name;
if (property_exists($this, $local_var_name)) {
return $this->{$local_var_name};
}
}
/**
*
* #param array $data The data to save
*/
public function save() {
// this means we're editing something
if ($this->id) {
$table = new My_Application_Product_Table();
$data = $table->find($this->id)->toArray();
$data = $data[0];
foreach (get_class_vars(get_class($this)) as $key => $value) {
if (! is_null($this->$key)) {
$data[preg_replace('/^_/', '', $key)] = $this->$key;
}
}
$id = $table->update($data, sprintf('id = %d', $this->id));
// this means we're creating, and this is the data we need
} else {
$data = array(
'id' => rand(1,1000000),
'name' => $this->name,
'date_created' => date('Y-m-d H:i:s'),
);
$id = $table->insert($data);
}
return $id;
}
}
'
class My_Application_Product_Table extends Zend_Db_Table_Abstract {
protected $_name = 'products';
protected $_primary = 'id';
}
Split your model in multiple classes :
1 class representing the entity (no methods, except for accessors).
this class represents your "real-life" object, and is just a structured data container, which encapsulates data
class My_Application_Model_Product {
protected $_id;
protected $_name;
protected $_company_id;
protected $_trade_names;
protected $_website;
//...
public function __set($name, $value) {
//Implement your setter here
}
public function __get($name) {
}
}
1 class responsible of data mapping.
This class makes is the link between your data source (database, webservice, file...) and your entity.
Class My_Application_Model_DataMapper_Product {
protected $_adapter
public function __construct($adapter)
{
$this->setAdapter($adapter);
}
public function setAdapter($adapter)
{
$this->_adapter = $adapter;
}
public function save(My_Application_Model_Product $product)
{
//Perform your save operation here
}
public function fetchAll()
{
}
public function findById($id)
{
}
//You may implement specific methods for any needed specific operation (search, bulk-update...
}
a third class for data access and persistence (Zend_Db_table, Soap client...) This third class is passed to the datamapper as the adapter and is used inside the methods to getch/save data.
With this architecture, you have a clear separation of responsibilities, and may change one part without affecting the other : for example, you could switch from a database to a webservice without affecting your Product class.
A very simple example is given in the zf Quickstart