Why is my function not working in phtml file?

By : may
Source: Stackoverflow.com
Question!

I have created a collection.php model and added a addOrderFilter() function, but when I try to call addOrderFilter() in my phtml file, it gives me

Fatal error: Call to a member function addOrderFilter() on boolean in [...]\magento\app\design\adminhtml\default\default\template\paketid\shipping.phtml on line 5

This is my shipping.phtml :

<?php echo $this->getChildHtml('PaketId_Shipping');?>
<h1>Test custom block</h1>
<?php $order = $this->getOrder()?>
<?php $shipping = Mage::getModel('paketid_shipping/result')->getCollection()->addOrderFilter($order) ?>
<?php if(count($shipping)):
foreach($shipping as $shipping): ?>
<?php echo $this->__('Booking Code') ;?>
<?php echo $shipping->getBookingCode(); ?>
<?php endforeach; ?>
<?php endif; ?>

My collection.php model :

class PaketId_Shipping_Model_Resource_Result_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
protected $order;

protected function _construct()
{
    $this->_init('paketid_shipping/result');
}

public function addOrderFilter($order)
{
    if ($order instanceof Mage_Sales_Model_Order) {
        $order = (int) $order->getId();
    }
    if (!is_array($order)) {
        $order = array($order);
    }

    $this->getSelect()->where("main_table.order_id IN (?)", $order);
    //$this->getSelect()->where("main_table.order_id IN (?)", $order)->order('id DESC');

    return $this;
}

The confusing part is, why does my phtml doesn't render my collection model ? Am I doing something wrong ?

By : may


Answers

Make sure magento is not running in compilation mode.

OR

There is something wrong in syntax Mage::getModel('paketid_shipping/result')->getCollection() . This is not returning correct collection object. Try debugging whether it returns collection object or not.

OR

You can try different similar syntax

Mage::getResourceModel('paketid_shipping/result_collection')->addOrderFilter($order);


I would suggest the following general design pattern. It wastes an additional pointer, but will enforce the requirement that the const object will be able to only access const methods of the private object:

class ObjectImpl;

class const_Object {

public:

  const_Object(const ObjectImpl* object_impl)
    : _impl(object_impl);

  // Only const methods

private:      
  const ObjectImpl* _impl;
};

class Object : public const_Object
{
  Object(ObjectImpl* object_impl)
    : const_Object(object_impl), _impl(object_impl);

  // non-const methods go here.

private:      
  ObjectImpl* _impl;
};


Adding to Ian's answer, Any complex element that is named by root element will have factory method annotated with @XmlElementDecl().

You can resolve this, by moving the complex type declaration inline like below.

<xsd:schema xmlns= "http://www.foo.org/bar" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    targetNamespace="http://www.foo.org/bar" jaxb:extensionBindingPrefixes="annox"
    jaxb:version="2.1" elementFormDefault="qualified">
    <xsd:element name="unit">
        <xsd:complexType>
            <xsd:annotation>
                <xsd:appinfo>
                    <annox:annotate>@javax.xml.bind.annotation.XmlRootElement(name="unit")
                    </annox:annotate>
                </xsd:appinfo>
            </xsd:annotation>
            <xsd:sequence>
                <xsd:any processContents="skip" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

ObjectFactory.class (no JAXBElement factory method generated here)

@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.foo.bar
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Unit }
     * 
     */
    public Unit createUnit() {
        return new Unit();
    }

}

Test class:

@Test
public void testUnmarshalling() throws JAXBException, SAXException {
    JAXBContext context = JAXBContext.newInstance(Unit.class);

    Unmarshaller unmarshaller = context.createUnmarshaller();

    unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(new StreamSource(getClass().getClassLoader().getResourceAsStream("common.xsd"))));

    Object unit = unmarshaller.unmarshal(getClass().getResourceAsStream("unit.xml"));

    System.out.println(">>> Class is: " + unit.getClass().getName());

    StringWriter writer = new StringWriter();
    context.createMarshaller().marshal(unit, writer);

    System.out.println(">>> XML is: " + writer.toString());

    //assertTrue(unit instanceof Unit);
}

Test xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<unit xmlns="http://www.foo.org/bar">
    <text>Name</text>
</unit>

output :

>>> Class is: org.foo.bar.Unit
>>> XML is: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><unit xmlns="http://www.foo.org/bar"><text>Name</text></unit>
By : ulab


This video can help you solving your question :)
By: admin