Get Primary Key out of Zend_Db_Table_Rowset Object - zend-framework

inside of my Zend_Db_Table_Rowset Object i found this:
["_primary:protected"]
... does anybody if theres a way to access this? ... maybe something like
$rowsetObject->getPrimary()
Thanks for your help,
Alex

Zend_Db_Table_Rowset has no property _primary. What you are refering to is either the Zend_Db_Table instance you got the Rowset from or a Zend_Db_Table_Row instance inside the Rowset.
For getting the primary key from a Zend_Db_Table instance you can do:
$tableInstance->info('primary')
For getting the primary key from a Zend_Db_Table_Row instance you can get the table instance and call info() on it:
$rowInstance->getTable()->info('primary')
Note that this will not work when the row is disconnected, because then getTable() will return null.
Or, when using a custom Zend_Db_Table_Row you can add a method that proxies to _getPrimaryKey():
class My_Db_Table_Row extends Zend_Db_Table_Row
{
public function getPrimaryKey()
{
return $this->_getPrimaryKey();
}
}

Since this variable is protected, you can extend Zend_Db_Table_Rowset and define getPrimary() function yourself, e.g.
class My_Zend_Db_Table_Rowset extends Zend_Db_Table_Rowset {
//put your code here
function getPrimary() {
return $this->_primary;
}
}

Related

Laravel Eloquent Models __construct method to call relations

I want to have my model automatically call its relations when instantiated. As of now my model looks like this:
class AdminLog extends Model{
public function __construct(){
$this->belongsTo('App\User', 'admin_id');
}
}
but when i try to do dd(AdminLog::get()->first());, it doesnt show any relations.
Edit#1: tried adding parent::__construct(); inside the model's __construct method but it didn't work.
belongsTo() defines a relationship, it doesn't load it.
First you need to define the relationship, then you can load it at any point using the load method.
class AdminLog extends Model {
public function user() {
return $this->belongsTo(\App\User::class, 'admin_id');
}
}
$log = AdminLog::first();
$log->load('user');
It is possible to load inside the constructor, but I would highly recommend against that. If you have 20 AdminLog objects then it will query the database 20 times, once for each object. That's inefficient.
What you should do instead is use eager loading. This will query the users table just once for all 20 admin logs. There are many ways to do this, here is an example:
$logs = AdminLog::take(20)
->with('user')
->get();
dd($logs->toArray());

How to store a unique value into a Panel (GWT)?

This is simple question but there's No answer found on the Internet.
Ok, some widgets such as CheckBox have a method called myCHeckBox.setFormValue(text); so I take advantage of this method to store the unique ID into a CheckBox so that later on I just myCHeckBox.getFormValue(); to retrieve back the unique value.
However, there's no setFormValue on GWT Panel?
So if we want to store a unique value into a Panel (for example, FlowPanel, VerticalPanel)?
Then Which method can i use to do that?
The way I see it what you are really trying is to extend the purpose of the Panel. I cannot see why you would want a unique identifier, as the object of the Panel is as unique as it guess but that's not the point here.
Since you want to extend the Panel do just that. Extend the corresponding class and give it a unique value, implement the corresponding getters and setters and then you are set. It is as simple as that
class AbsolutePanelUnq extends AbsolutePanel
{
private int uniqueId;
public getUniqueId(){
return uniqueId;
}
public setUniqueId(int uniqueId)
{
this.uniqueId = uniqueId;
}
}
Then create the object and do whatever you need.
Best way I can suggest:
Public class myPanel extends Panel {
private myUniqueValue;
//Getter setter for myUniqueValue
}

Provide an implementation to a typed factory's method

I have a typed factory interface as follows:
public interface ILogMessageFactory
{
ILogMessage Create(LogMessageType logMessageType, String text);
}
and I am registering it all follows:
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For(typeof(ConsolePrompter)),
Component.For<ILogger>().ImplementedBy<ConsoleLogger>().LifeStyle.Transient,
Component.For<ILogMessageFactory>().AsFactory(),
Component.For<ILogMessage>().ImplementedBy<LogMessage>().LifeStyle.Transient
);
}
The problem is that I want to implement the ILogMessageFactory.Create method myself, to set a few things before I return.
I've tried the obvious naïve solution without any success:
Component.For<ILogMessageFactory>().ImplementedBy<LogMessageFactory>().AsFactory()
Am I approaching this wrong? Should I just keep all initialization in the constructor of the given object?
use ITypedFactoryComponentSelector if this is something that really belongs to the factory. Alternatively use .OnCreate() on the component the factory resolves

Google GIN AbstractGinModule & GWT.Create()

I have a class that extends AbstractGinModule
like:
public class ClientModule extends AbstractGinModule {
public ClientModule() { }
#Override
protected void configure() {
...
...
bind(...class).annotatedWith(...).to(...class).in(Singleton.class);
...
}
}
The idea that I have is to bind one class with another class based on a value stored in a property file.
like:
param contains the value coming from the property file
if(param.equals("instanceB"))
bind(a.class).to(b.class)
else
bind(a.class).to(c.class)
I have a class that access this property file and return a string with the value.
This class is called: InstanceParameters.java
I would like to get an instance of this class within my ClientModule.
But I don't find any way to do it.
I tried with:
- InstanceParameters param = new InstanceParameters ();
- GWT.create(InstanceParameters.class); (Error because this method should only be used on the client side)
Is there a way to access this InstanceParameters class within this clientModule?
Thank you for your help
You don't need to read the file before launching the application - just before creating the AbstractGinModule (via GWT.create). So, load the Dictionary in your onModuleLoad method and pass the parameters, either as a whole InstanceParameters class or as the extracted String, via a provider or any other means.

row specific class

How do I create a Zend_Db_Table which returns a different class for each row.?
Example
UserTable has id,name and type
Type contains class names (admin,client,etc...)
The classes admin, client are all subclasses of user
If I call fetch I need to get a admin or client object depending on the corresponding value in the db.
class Your_Db_Table_Row extends Zend_Db_Table_Row_Abstract
{
}
class Your_Db_Table extends Zend_Db_Table_Abstract
{
protected $_rowClass = "Your_Db_Table_Row";
}
or
new Your_Db_Table(array("rowClass" => "Your_Db_Table_Row");
So whenever you get a rowset from your table subclass, the rows included in it will be your custom class.
Edit
To get a custom row based on a value, I would say extend the Zend_Db_Table_Rowset_Abstract class instead and override this method:
getRow(int $position, [bool $seek = false])
You'll also need to override the current method and perhaps some of the other SeekableIterator implemetations which actually creates a row class based on the _rowClass property. You might be able to set the _rowClass before current is called based on your data's row type.
You could instantiate a specific class in current and return it based on the type parameter.
Have you though about maybe just using composition instead? Say just passing in data to a new class if it's an admin type or something?