cakephp3 Class DateTime not found - cakephp

I'm trying to convert my cakephp plugin from 2.X to 3.
I have a little problem inside my component plugin at this line:
$dStart = new DateTime($now); //$now = date('Y-m-d H:i:s');
return me this error:
Error: Class 'CurrencyConverter\Controller\Component\DateTime' not found
Seems that it search DateTime inside my plugin directory.
How can I solve it?
Thanks

Try:
$dStart = new \DateTime($now);
because CakePHP 3.0 using namespaces and if you not add a root namespace it will be looking for class under current namespace.

Related

How to get the published posts in laravel using corcel?

I am trying to use corcel to retrieve data from wordpress in laravel.
if i use like this, it show the error like this
$posts = \App\Post::published()->get();
return $posts;
BadMethodCallException in Builder.php line 2451: Call to undefined
method Illuminate\Database\Query\Builder::published()
But if i use like this
$posts = \App\Post::all();
return $posts;`
it works.Help me out where am i missing to do.
Thanks in advance.
The problem is that you are still extending the Laravel model instead of Corcel. Since the published() method is defined in Corcel instead of Model you get that error.
Your Post class should look like this: (from the docs)
<?php // File: app/Post.php
namespace App;
use Corcel\Post as Corcel;
class Post extends Corcel //this should be Corcel and not Model
{
protected $connection = 'wordpress';
}

CakePHP 3 Error on using SplFileInfo class

i'm getting the next error:
Error: Class 'App\JL\SplFileInfo' not found while trying to create a SplFileInfo class like this: $i = new SplFileInfo($P['Carpeta'].DS.$v);
i tested this with same results: $i = new SplFileInfo('');
this is in a cutom class called JL in namespace App\JL and calling it like use App\JL\JL;
The function is declared like this: public static function LeerArchivos(&$P = array()) {
i'm working in an element to be shown on a template...
¿Do i have to activate somethig? or ¿What am i doing wrong?
The problem is that you don't know how namespaces work. You should fix that gap in your knowledge first: http://php.net/manual/en/language.namespaces.php
You need to "import" the class from the "root" namespace \ into your current namespace \App\JL\JL. This us done via the use keyword. See the documentation for it.

Undefined Property: Security::$table [duplicate]

Hey I have coded CakePHP for a number of things but never ran into this problem before surprisingly. Also I have thoroughly searched the net and CakePHP docs and have not found an answer to my question. My question is, I have a table for my model that should be named Class, obviously I cannot use that name though since it's a reserved PHP keyword. What options do I have to be able to refer to this model appropriately.
So far I have;
Renamed my class model file to player_class.php
Renamed my class model class to PlayerClass
Changed var $name to 'PlayerClass'
Added to my class model class; var $useTable = 'classes';
Renamed my class controller to player_classes_controller.php
Renamed my class controller class to PlayerClassesController
Changed var $name to 'PlayerClasses'
While this does work, is this what has to be done or are to other options to be able to refer to it as Class still, like can I do any sort of mangling like _Class?
I once tested all CakePHP class names for Cake 1.2 if they can be used as Model names, here are the results:
NOT possible is:
app
appcontroller
appmodel
behaviorcollection
cache
cacheengine
cakelog
cakesession
classregistry
component
configure
connectionmanager
controller
datasource
debugger
dispatcher
file
fileengine
folder
helper
inflector
model
modelbehavior
object
overloadable
overloadable2
router
security
sessioncomponent
set
string
validation
Possible is:
acl
aclbase
aclbehavior
aclcomponent
aclnode
aclshell
aco
acoaction
admin
ajaxhelper
apcengine
apishell
app_model
apphelper
aro
authcomponent
bake
baker
bakeshell
behavior
cachehelper
cake
cakeschema
cakesocket
consoleshell
containablebehavior
controllertask
cookiecomponent
dbacl
dbaclschema
dbconfigtask
dboadodb
dbodb2
dbofirebird
dbomssql
dbomysql
dbomysqlbase
dbomysqli
dboodbc
dbooracle
dbopostgres
dbosource
dbosqlite
dbosybase
element
emailcomponent
error
errorhandler
extracttask
flay
formhelper
htmlhelper
httpsocket
i18n
i18nmodel
i18nschema
i18nshell
iniacl
javascripthelper
jshelper
jshelperobject
l10n
layout
magicdb
magicfileresource
mediaview
memcacheengine
modeltask
multibyte
numberhelper
page
pagescontroller
paginatorhelper
permission
plugintask
projecttask
requesthandlercomponent
rsshelper
sanitize
scaffold
schema
schemashell
securitycomponent
sessionhelper
sessionsschema
shell
shelldispatcher
test
testsuiteshell
testtask
texthelper
themeview
timehelper
translate
translatebehavior
treebehavior
viewtask
xcacheengine
xml
xmlelement
xmlhelper
xmlmanager
xmlnode
xmltextnode
When i run into this sort of problem i usually do what you did, only i prefix the reserved word with "My" (so when i read the code it doesn't look like that class has anything to do with "Player"... for example, just the other day i wanted to model a "ACO" model.. but that already existed in cake (same scenario of reserved word) so i created a model called Myaco.
I think you should just name it Myclass.
Regarding the model name and controller name changes- i think you did good, i would do the same. Your only real option is to use the $useTable = 'classed'; to use your DB table.
If you use the underscore prefix, i believe cake will not be able to handle it (it will fail in the Inflector class).
Good luck
I can second that solution. I had the same problem and used a prefix that was the initials of the client. Ended up calling mine Dtclass. Unfortunately, it took me an hour or so to figure out what the problem was. One of those cases where the answer stares you in the face all the time till you finally recognize it.

CakePHP 3.0 vendor class not found

I'm adding an external class to a cake 3.0 app by putting it to /vendor/name folder and requiring it from a component like this:
require_once( $_SERVER['DOCUMENT_ROOT'].'/project/vendor/external/testClass.php');
But when I try to getInstance(); of a class - I get an error
Class 'App\Controller\Component\Test_Class' not found
I am calling this from a component (thus the \Controller\Component).
What is it that i'm doing wrong?
CakePHP 3.0 uses namespaces. So use proper namespace for your vendor class or if it's not using namespaces prefix the class name with backslash when using it.
E.g. $object = new \Test_Class();.

CakePHP override plugin method

I'm using the CakeDC Tags plugin in my CakePHP project. I am using the TagCloudHelper to output a list of links but this helper isn't formatting the links the way I would like. Specifically the method _tagUrl($tag, $options) is generating links with named parameters instead of using the query string.
Instead of
$options['url'][$options['named']] = $tag[$options['tagModel']]['keyname'];
I get the desired behavior with the following change
$options['url']['?'][$options['named']] = $tag[$options['tagModel']]['keyname'];
what is the best practice way of overriding this method? Do I make another class that extends TagCloudHelper? How then would I tell CakePHP to use my class instead of the plugin's?
I was able to override the helper with my own
// app/View/Helper/MyTagCloudHelper.php
App::uses('TagCloudHelper', 'Tags.View/Helper');
class MyTagCloudHelper extends TagCloudHelper {
protected function _tagUrl($tag, $options) {
$options['url']['?'][$options['named']] = $tag[$options['tagModel']]['keyname'];
return $options['url'];
}
}
Then in the controller
public $helpers = array('TagCloud' => array('className'=>'MyTagCloud'));
the className option let's you alias the name of the helper

Resources