I have three two classess
class Application_Model_Accounts extends Zend_Db_Table_Abstract
{
protected $_name = 'accounts';
protected $_dependentTables = array('Application_Model_Bugs');
}
And
class Application_Model_Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_dependentTables = array('Application_Model_BugsProducts');
protected $_referenceMap = array(
'Reporter' => array(
'columns' => 'reported_by',
'refTableClass' => 'Application_Model_Accounts',
'refColumns' => 'account_name'
),
'Engineer' => array(
'columns' => 'assigned_to',
'refTableClass' => 'Application_Model_Accounts',
'refColumns' => 'account_name'
),
'Verifier' => array(
'columns' => array('verified_by'),
'refTableClass' => 'Application_Model_Accounts',
'refColumns' => array('account_name')
)
);
}
in index controll i am trying to run this code.
public function indexAction()
{
$accountsTable = new Application_Model_Accounts();
$accountsRowset = $accountsTable->find(1234);
$user1234 = $accountsRowset->current();
$bugsReportedByUser = $user1234->findDependentRowset('Application_Model_Bugs');
}
and on line
$bugsReportedByUser = $user1234->findDependentRowset('Application_Model_Bugs');
I am getting this error
An error occurred
Application error
I am unable to findout the problem. How to fix this problem. and is there a way to get more developer friendly error in Zend rather then just getting this message "An error occured".
I figure out the solution for this problem.
in .Htaccess file
enable the development mode by adding this line on top
SetEnv APPLICATION_ENV "development"
This will show the complete error trace.
Sorted
full working example of bugs code including db available here
http://phphints.wordpress.com/2010/06/25/zend-framework-finddependentrowset-and-findparentrow-demo/
Related
My Custom Zend Form Filter Classes are not being loaded.
The Custom Filter Class name is: SF_Filter_AlnumDashes and it resides into:
library/SF/Filter/AlnumDashes.php.
I have configured autoloading for the "SF_" namespace into application.ini:
autoloadernamespaces[] = "SF_"
But when I try to load the Class during Zend Form creation I get error that the class coudn't be found:
Fatal error: Class 'AlnumDashesUnderscore' not found in...
Here is the code that is causing the error in the Zend Form class:
class Admin_Form_Product_Generalinfo extends SF_Form_Abstract {
public function init() {
//set ID Attribute on the form element
$this->setAttrib('id', 'product_general_info');
$elementPrefixPaths = array(
array(
array(
'prefix' => 'SF_Filter',
'path' => 'SF/Filter/', // 'application/validators' in your case
'type' => 'filter',
)
)
);
$this->addElementPrefixPaths($elementPrefixPaths);
$this->addElement('text', 'title', array(
'filters' => array('StringTrim', 'StripTags'),
'validators' => array(
array('StringLength', true, array(2, 250)),
),
'required' => true,
'label' => 'Title',
'attribs' => array(
'id' => 'title',
'class' => 'inputbox'
)
));
$this->getElement('title')->addFilter(array(new AlnumDashesUnderscore(), array(1)));
}
I have other classes into "SF_" Namespace that get load successfully, calling them from Controllers with no problem.
The class is defined as SF_Filter_AlnumDashes, but you're trying to create a new instance of AlnumDashesUnderscore. These class names need to match:
$this->getElement('title')->addFilter(array(new SF_Filter_AlnumDashes(), array(1)));
i developing a site with zend framework.
i use autoload for load a class.
it work on controller, on model but not work in bootstrap file.
why?
bootstrap.php
protected function _initAutoload ()
{
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array('basePath' => APPLICATION_PATH, 'namespace' => '',
'resourceTypes' => array(
'form' => array('path' => 'forms/', 'namespace' => 'Form_'),
'model' => array('path' => 'models/', 'namespace' => 'Model_'),
'plugin' => array('path' => 'plugin/', 'namespace' => 'Plugin_'))));
// viene restituto l'oggetto per essere utilizzato e memorizzato nel bootstrap
return $autoLoader;
}
/**
* inizializza l'autenticazione
*/
protected function _initAuth ()
{
$this->bootstrap("db");
$this->bootstrap("Autoload");
$db = $this->getPluginResource('db')->getDbAdapter();
$adp = new Zend_Auth_Adapter_DbTable($db);
$adp->setTableName(USERS_TABLE)
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('sha1(?)');
$storage = new Model_Sessions(false, $db);//line 81
$auth = Zend_Auth::getInstance();
$auth->setStorage($storage);
//$this->bootstrap('log');$log=$this->getResource('log');
if ($auth->hasIdentity()) {
$identity = $auth->getIdentity();
$user = $identity->user_id;
} else
$user = 1;
$user = new Model_user($user);
}
output error
Fatal error: Class 'Model_Sessions' not found in /application/Bootstrap.php on line 81
in Session.php
<?php
/**
* #method get($k,$dv=FALSE)
*/
class Model_Sessions implements Zend_Auth_Storage_Interface
{
Your resource autoloader looks good.
I suspect you want Model_Sessions, not Model_sessions (not lower/upper case on "sessions").
Make sure the class Model_Sessions is stored in file application/models/Sessions.php
As a side note, you have your resource autoloader looking for plugins with namespace prefix plugins_. Again, here, I suspect you want uppercase Plugins_.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to integrate zf2 beta3 with doctrine mongo odm (https://github.com/doctrine/DoctrineMongoODMModule) but no sucess.
How can I install and configure it?
I'm doing just the same thing. Something like this should work:
Download the module, and place in your vendor folder.
Add the module in application.config.php
Copy module.doctrine_mongodb.config.php.dist to /config/autoload
Edit that config file with your own settings
Change the name of that config file to module.doctrine_mongodb.local.config.php
Create a 'setDocumentManager' method in your controller like this:
protected $documentManager;
public function setDocumentManager(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
return $this;
}
Place the following in your module's DI config:
'Application\Controller\[YourControllerClass]' => array(
'parameters' => array(
'documentManager' => 'mongo_dm'
)
),
Create Document classes according to the Doctrine 2 documentation, and the clarification in this question and answer: Annotations Namespace not loaded DoctrineMongoODMModule for Zend Framework 2
Finally, use the dm like this:
public function indexAction()
{
$dm = $this->documentManager;
$user = new User();
$user->set('name', 'testname');
$user->set('firstname', 'testfirstname');
$dm->persist($user);
$dm->flush();
return new ViewModel();
}
I will give the steps I have done to integrate zf2 with mongodb doctrine odm
1.Download the mongodb doctrine odm module and place in vendor directory or clone it from github
cd /path/to/project/vendor
git clone --recursive https://github.com/doctrine/DoctrineMongoODMModule.git
2.Copy the file from /path/to/project/vendor/DoctrineMongoODMModule/config/module.doctrine_mongodb.config.php.dist, place in your path/to/your/project/config/autoload/ and rename to module.doctrine_mongodb.local.config.php
3.Edit your module.doctrine_mongodb.local.config.php.
Change the default db
'config' => array(
// set the default database to use (or not)
'default_db' => 'myDbName'
),
Change your connection params
'connection' => array(
//'server' => 'mongodb://<user>:<password>#<server>:<port>',
'server' => 'mongodb://localhost:27017',
'options' => array()
),
Change the driver options
'driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'namespace' => 'Application\Document',
'paths' => array('module/Application/src/Application/Document'),
),
Add proxy and hydratros config
'mongo_config' => array(
'parameters' => array(
'opts' => array(
'auto_generate_proxies' => true,
'proxy_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Proxy',
'proxy_namespace' => 'Application\Model\Proxy',
'auto_generate_hydrators' => true,
'hydrator_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Hydrators',
'hydrator_namespace' => 'Application\Document\Hydrators',
'default_db' => $settings['config']['default_db'],
),
'metadataCache' => $settings['cache'],
)
),
4.Create a directory named "Document" in /path/to/project/module/Application/src/Application/ where goes your documents mapping and inside "Document" directory, create "Proxy" and "Hydrators" directories.
5.Edit your /path/to/project/config/application.config.php and add 'DoctrineMongoODMModule' to modules array
6.Be sure you have mongo php extension installed otherwise download at http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows and copy it to your extension php directory, usually /php/ext. Add the extension line acording the name file extension you have downloaded "extension=php_mongo-x.x.x-5.x-vc9.dll" in your php.ini.
7.Create a document mapping User.php in your document directory application module.
<?php
namespace Application\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** #ODM\Document */
class User
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="string") */
private $name;
/**
* #return the $id
*/
public function getId() {
return $this->id;
}
/**
* #return the $name
*/
public function getName() {
return $this->name;
}
/**
* #param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* #param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
}
8.Persist it, for example in your controller
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\ActionController,
Zend\View\Model\ViewModel,
Application\Document\User;
class IndexController extends ActionController
{
public function indexAction()
{
$dm = $this->getLocator()->get('mongo_dm');
$user = new User();
$user->setName('Bulat S.');
$dm->persist($user);
$dm->flush();
return new ViewModel();
}
}
Now the default config has changed, could you show an updated method to get this working in ZF2?
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'user' => null,
'password' => null,
'dbname' => 'user',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => null,
'filters' => array() // array('filterName' => 'BSON\Filter\Class')
)
),
'driver' => array(
'odm_default' => array(
'drivers' => array()
)
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
Currently receiving the error: The class 'Application\Document\User' was not found in the chain configured namespaces
I'm trying to get a simple login form to work using CakePHP 2.0... just Auth, no ACLs for now.
I'm able to see the form and enter the email and password as they are in the database, but I just get returned to the form and the flash error message is displayed. Here is my code:
AppController:
class AppController extends Controller
{
function beforeFilter()
{
$this->Auth->userModel = 'Users';
$this->Auth->fields = array('username' => 'email', 'password' => 'password'); //have to put both, even if we're just changing one
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'hotels', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
}
}
login.ctp:
<?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
UsersController:
class UsersController extends AppController
{
var $name = 'Users';
var $helpers = array('Html','Form');
var $components = array('Auth','Session');
function beforeFilter()
{
$this->Auth->allow("logout");
parent::beforeFilter();
}
function index() { } //Redirects to login()
function login()
{
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
} else
{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
function logout()
{
$this->redirect($this->Auth->logout());
}
}
?>
I appreciate any help with this. Thanks!
The "Invalid username or password, try again" error is displayed after you hit login?
There are a few things you should check:
• Is the output of $this->Auth->login() identical to the information in your database? Put debug($this->Auth->login()) to see the output in your login method after the form is submitted.
• Are the passwords correctly hashed in the database?
• Try making the AuthComponent available to all your controllers not just the UsersController.
• Not sure if this makes a difference, but call parent::beforeFilter(); before anything else in your controller's beforeFilter method.
EDIT:
Is see that you're trying to validate based on email and password. As a default AuthComponent expects a username and password. You have to explicitly state that you want the email and password to be validated by $this->Auth->login(). This comes from the 2.0 documentation:
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
The fact that you're not seeing any SQL output is to be expected, I believe.
Also you must check if your field "password" in database is set to VARCHAR 50.
It happens to me that I was truncating the hashed password in DB and Auth never happened.
if you are not using defalut "username", "password" to auth, you cant get login
e.g., you use "email"
you should edit component declaration in your controller containing your login function:
$component = array('Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'mot_de_passe')
)
)
));
Becareful with cakephp's conventions. You should change this "$this->Auth->userModel = 'Users';" to "$this->Auth->userModel = 'User';" because User without plural is the Model's convention in cake. That worked for me and also becareful with the capital letters. it almost drived me crazy. Good luck.
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'Events',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'Users',
'action' => 'login',
'home'
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username','password' => 'password')
)
)
)
);
Editing the component declaration in AppController did the trick for me. If you have the fields named other than "username" and "password" you should always specify them. In your case it would be
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username' => 'email','password' => 'password')
)
)
)
);
There is a bug in the cakephp tutorial.
$this->Auth->login() should be changed to
$this->Auth->login($this->request->data)
I was trying an example of one to many relationship in zend framework, but I can't get results.
Here are the tables:
UsersTable:
<?php
class Application_Model_DbTable_UsersTable extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_dependentTables = array('Application_Model_DbTable_BugsTable');
}
BugsTable:
class Application_Model_DbTable_BugsTable extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_dependentTables = array('Application_Model_DbTable_BugsProductsTable');
protected $_referenceMap = array(
'Reporter' => array(
'columns' => 'reported_by',
'refTableClass' => 'Application_Model_DbTable_UsersTable',
'refColumns' => 'username'
),
'Engineer' => array(
'columns' => 'assigned_to',
'refTableClass' => 'Application_Model_DbTable_UsersTable',
'refColumns' => 'username'
),
'Verifier' => array(
'columns' => array('verified_by'),
'refTableClass' => 'Application_Model_DbTable_UsersTable',
'refColumns' => array('username')
)
);
}
As you can see, this is one to many relationship from db table 'users' to table 'bugs' , where we have three foreign reference keys in bugs talbe.
Now when I try to use Zend methods for one to many relationships I always get empty results:
$tableUser = new Application_Model_DbTable_UsersTable();
$tableBugs = new Application_Model_DbTable_BugsTable();
$result= $tableUser->find(1);
$user= $result->current();
$userBugs = $user->findDependentRowset('Application_Model_DbTable_BugsTable','Verifier');
echo count($userBugs); //returns 0
$bugresult = $tableBugs->find(1);
$thisbug= $bugresult->current();
$verifier= $thisbug->findParentRow('Application_Model_DbTable_UsersTable','Verifier');
return $verifier //returns nothing
The proper data is in the database, when I for example do a query:
select * from bugs b, users u where b.reported_by=u.id and u.id=1;
I get the expected results. But when I try in zend no results.
Do you have any suggestions? Thanks.
In your sql u get bugs by field
reported_id
but in this code:
$result= $tableUser->find(1);
$user= $result->current();
$userBugs = $user->findDependentRowset('Application_Model_DbTable_BugsTable','Verifier');
echo count($userBugs); //returns 0
$bugresult = $tableBugs->find(1);
$thisbug= $bugresult->current();
$verifier= $thisbug->findParentRow('Application_Model_DbTable_UsersTable','Verifier');
return $verifier //returns nothing
u try to get bug by
verified_by
field
try this:
$result= $tableUser->find(1);
$user= $result->current();
$userBugs = $user->findDependentRowset('Application_Model_DbTable_BugsTable','Reporter');
echo count($userBugs);