yii framework models database access object - database

I want to use the database access objects to access a table in my database instead of doing active record since that takes a long time to load. if I use database access objets instead (calling createCommand, query, execute, etc), do i still need to create a model class for the table? if so what would be the parent class of this model class? My goal is to access/edit the table values using yii database access objects. I'm using YII FRAMEWORK. Or is it best to use a component? If so, when do you usually have to use components? I don't understand what components are for....

No, you can create a generic class which has a member property of a CDbConnection. For that matter, you can just use CDbConnection, but you might end up creating a lot of connections that way.
class Foo
{
private $conn;
function __construct(){ $this->conn =
new CDbConnection($dsn,$username,$password); }
function runQuery($sql) {
$command=$connection->createCommand($sqlStatement);
return $command->query();
}
}

If you have your database settings in your config/main.php file, you can also do:
$command = Yii::app()->db->createCommand($sql);
$result = $command->queryAll();
and if you need to reset the command:
$command = false;
If you create a custom class, you don't need to reference a Model or extend an existing class, your connection will use CDbConnection (by using the method above or calling it directly in your connection statement).
this page has pretty clear info on Yii's DAO.
As far as components go, they can mean different things - there are Yii's "core components", which are things like urlManager, user, db, etc. and can have their default properties set in the config/main.php file. Then there is the "components" directory which can be configured to autoload classes and "contains components (e.g. helpers, widgets) that are only used by this application." So you can put custom classes in there that you want available throughout your app.

Components are used to make widgets.which is available through out the app.

Related

CodenameOne: Is it possible to define 'Views' for different usergroups of the app?

I am currently planning to develop an app using CodenameOne and I was wondering if if was possible to define some database like views onto an app, so that different users see different parts of the app?
I am talking about an application, that is part of a project structure, that is made up of different modules, which need to be bought separately, but should all be integrated into the same app. As some kind of advertising strategie, the unbought parts should be displayed, but have no functionality.
Another question, closely related to this would be how to let different people use different GUIS of the same app?
Example: There is a master and a worker.
Master has these menus:
- show all machines
- show available teams
- send message to team x
Worker has these menus:
- show my machine
- show my team
- send message to master
If the master reassignes a team, the team members menu's contents would just change accordingly, without the app itself changing.
if a worker gets assigned as master, the server would send something like you are now the master and the view would change to the masters view (still in the same app)
Is this possible?
I believe what you might be needing is just to build those views dynamically. If the application logic says that a certain user is the master - then you build a master-like view for that user otherwise you create simple worker view. Regarding the assignation (which master rules a worker), it is just the basic one-to-many relationship paradigm.
Tips on building your views dynamically with GUI + Code :
public UserViewBuilder extends UIBuilder {
private Resource res;
private Container view;
//User is the parent class of both Worker and Master
private User user;
...
public UserView(Resource res){
this.res = res;
}
private void init(boolean isMasterView){
//where you initialize all the component of your view
}
...
public Container viewSelector(boolean isUserMaster){
if(isUserMaster){
//getting data for a master-user, just throwing ideas
this.user = Data.fetchYourMasterData();
//getting the design from GUI
this.view =this.createContainer(res,"ContainerDesignForMaster");
//putting the necessary data into the design
this.init(isUserMaster);
}
else {
//getting data for a worker-user, just throwing ideas
this.user = Data.fetchYourWorkerData();
//getting the design from GUI
this.view =this.createContainer(res,"ContainerDesignForWorker");
//putting the necessary data into the design
this.init(isUserMaster);
}
}
...
}
Using that architecture approach you will create the design for master and worker with the GUI builder - then you will code the view selection logic.
(Let me know if you need a working sample netbeans project for better understanding)
There is no builtin support for something like that if I understand you correctly. You could probably use a set of permissions system and if statements to achieve that but there is no system where you can explicitly say "this module is only visible to that user".

Proper way to access entity key outside the original class

I'm coding my first GAE web app in Python. I need to collect ~30 properties and instantiate a Client object (I'm using Model.db). I'd like to accomplish this on 4 separate sequential forms. If I use a separate page handler for each form, what is the best way to extract and reference the key or id on each form and put data into the same data entity? How do I avoid a global variable?
Not sure I follow... You may need to explain the issue a little more clearly.
First, do not use a global variable.
You can have 4 forms on the same template, all with the same handler. Give each a separate hidden attribute. Or, depending on your templating package, you should be able to access the form by ID.
In your handler:
entity = MyModel.get_by_key_name('my_key_name')
if request.method == 'POST':
if request.POST['hidden_field'] == 'firstform':
entity['prop_1'] = form.prop_1.data
...
if request.POST['hidden_field'] == 'secondform':
...
...
entity.put()
else:
return <template>
You will need to modify the syntax above to suit your templating pkg.

Dynamically passing a model name to a CakePHP Plugin

I'm having trouble wording my problem, so it's been tough to search for an answer. Hopefully you'll know how to help.
I am creating a CakePHP 2.1 Plugin that will interact with a series of its own Models:
- Friend
- Group
- User
Friend and Group are models that are created specifically for the Plugin, and they function within the plugin normally. However, the User model is really just an alias for some other table in the parent app.
So, if "My Awesome Application" decides to use "My Awesome Plugin", it will have to have its own "users" table (though it may called something else). Let's say "My Awesome Application" has a Model called MyUser. "My Awesome Plugin" wants to dynamically tell its internal User model to $useTable = "my_users".
My question is, how do I pass that data to the Plugin? How do I configure "My Awesome Plugin" to understand that User should $useTable "my_users";
As I understand you would like a Model in a PlugIn to use a table that would typically belong to a Model in your Application - by the conventions. Have you tried statically setting:
public $useTable = "my_users";
in the plugin? All plugins usually get initialized when Cake starts up, so all configurations should be loaded then. Why do you need this - it does really restrict you a lot? Will the table being used by the Plugin model change runtime?
The Model class also has some goodies you may find useful:
$this->User->table;
holds the table name for the model - the table that is currently being used that is**.
Also you can set the source table for the Model (inside a Controller) with:
$this->User->setSource('table_name);
** I am not sure if this applies when you use Model::setSource(). It would be interesting to check out what $this->User->table; holds after a Model::setSource() call.
I've figured out a way to accomplish this, but it might not work in all scenarios for all people.
I created a Component in my Plugin, and then I call the Component in my Controller. I pass the name of the users Model through the Component. This way, I can get information about the users Model, and I can set it as the useTable to my Plugin for use in the Plugin.
Of course, this method restricts me to using the Component to utilize the Plugin, but that's probably for the best.
Here's an example of how I did it:
// in the AppController
public $components = array(
'MyPlugin.MyPluginComponent' => array('userModel'=>'UserModelName')
);
// in the Plugin's Component
class MyPluginComponent extends Component {
function initialize($controller) {
//get the base user model
$this->UserModel = ClassRegistry::init($this->settings['userModel']);
//set the useTable for our plugin's user model
$this->PluginUser = ClassRegistry::init('MyPlugin.PluginUser');
//set the useTable value for this model
$this->PleaseUser->setSource($this->UserModel->useTable);
}
That seems to work for me. Hope this helps someone else.

CakePHP a class that can be used anywhere?

I have a multi-domain site. Depending on the domain the site needs to behave accordingly.
I created a helper called CompanyInfo it has methods such as name(), phone(), email(), etc.
Depending on what domain you are on it returns the correct information.
So for example if I need to display the phone number for a user to call I would use $this->CompanyInfo->phone() and it will display the correct phone number for the user depending on the domain.
Ok, this is all good, but not really relevant. The real issue is, I need this information in more than just the view. Helpers are just for views though. If I want to access this information from a controller I need to create a component to do it.
I really don't want to have a Helper and a Component doing the same thing. I would rather have one class handle it rather than copy and paste logic.
So whats the best way to have a class with methods that can be accessed from the controller or view or even model?
Is it just this kind of static information (name, phonenumber, email, etc) what you need to display? Why not just add them to your configuration in core.php?
Something like
# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', 'joe#acme.com');
You can then get this info anywhere you need using
Configuration::read('Company.name');
You can define this variable in your app_controller and then use these variable easily in any of your controller as set these variables from there only.
Call this function in your construct class.
i think that will solve your problem.
You can access model classes from any place this way :
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
a) you can use libs in cake1.3 for that
b) static model methods which you can pass the content to and which will return the expected value
echo Model::phone($data)

Silverlight / .NET RIA Services - Exposing a custom property to the client

I have a table in my database called "Task". Task has the following fields:
- ID
- Description
- AssignedUserID
- TaskTypeID
I am accessing this table through a class that was created automatically after I used an ADO.NET Entity Data Model. I can load and show the fields mentioned above in a DataGrid in my Silverlight application. However, AssignedUserID and TaskTypeID are not very descriptive. So I decided to create a stored procedure that gets the tasks and the user and task type names through their respective lookup tables. This is where the problem lies.
I want to create some custom properties in the automatically generated "Task" class. The custom properties would be named "AssignedUserName" and "TaskType". I then want to make these properties available to my Silverlight client. However, I cannot seem to figure out how to get them exposed to my Silverlight client.
Can someone help?
Thank you
If your EDM is in the same project as the DomainService you can do this:
create a partial class on the Entity type, and add your calculated property in there.
name the file **.shared.cs
it will then be auto-shared with the client/Silverlight code.
Edit:
I was assuming that you could do this calculation in app logic rather than use an sp, which seems more straightforward to me.
If you do use an SP, you'll need to use the Function Import feature in the designer to map the SP to a function in the EDM. This function can then return entities, with properties mapped however you like.
An easier way would be to just use the object model: Have Task.AssignedUser and Task.TaskType objects off of your Task class. Map these to lookup tables in your db. This will work out-of-the box (assuming the Id's are FK's to those lookup tables).
So, a couple options:
use app-logic--properties in a partial class to return the descriptions
use the object model driven by FKs to lookup tables, then just access Task.AssignedUser.Name or Task.TaskType.Description
use a function import to access the SP and map the returned values to entity properties
1 or 2 being the best options IMHO.
Another approach might be to update your EF model to include the lookup tables, add Associations between the tables, add [Include]s in the (auto-gen'd) metadata class and let EF and RIA do it for you. Maybe.

Resources