Silverstripe 4, How to solve database table conflict that maps to same database table - silverstripe

So I made a mistake and accidentally defined two classes that lead to the same database table
<?php
namespace Sca {
use SilverStripe\ORM\DataObject;
class Gallery extends DataObject {
private static $table_name = 'Gallery';
// ...
}
}
And the other class
<?php
namespace Sca {
use SilverStripe\ORM\DataObject;
class GalleryHolder extends DataObject {
private static $table_name = 'Gallery';
// ...
}
}
Then I ran /dev/build and the site crashed. Now the only thin I see is the apache error on apache log under /var/log/apache/..
[2019-08-05 23:36:32] error-log.ERROR: Uncaught Exception
LogicException: "Multiple classes ("Sca\GalleryHolder", "Sca\Gallery")
map to the same table: "Gallery"" at
/var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php
line 299 {"exception":"[object] (LogicException(code: 0): Multiple
classes (\"Sca\GalleryHolder\", \"Sca\Gallery\") map to the same
table: \"Gallery\" at
/var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299)"}
[]
I dumped the database and $ grep'ed the dirs with no success to find the place where there is defined GalleryHolder that leads to creating Gallery table for that class. No results. I also deleted the class file for GalleryHolder and ran again /dev/build but still gas that error and "Server error" screen when visiting from the web.
Is there any suggestions where to clear the cache or schema to resolve the conflict?

So I found a quick fix. I went to /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299
and commented out the lines that was throwing an error and added new line that modifies the table name if there is conflict. I reset changed these lines
if ($conflict) {
// throw new LogicException(
// "Multiple classes (\"{$class}\", \"{$conflict}\") map to the same table: \"{$table}\""
// );
$table = $table . '_dupl';
}
Then I was able to load the page again and it rebuilt the cache by it self and I uncomented previousley commented out lines at /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299 .

Related

Zend\Permissions\Rbac\Role Error 500 when try to instantiate object

I'm trying to implement a RBAC in my project, but I can't instantiate Rbac class.
My code:
<?php
namespace Login\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Permissions\Rbac\Rbac;
use Zend\Permissions\Rbac\Role;
class TesteController extends AbstractActionController {
public function indexAction() {
$role = new Role('teste');
die('=== FIM ===');
}
}
When I comment the Role line it shows "=== FIM ===", but when it isn't commented it gives 500 error.
I already check the module struct in vendor and it's alright.
Do I need to do anything else when I install a vendor module via composer to zend 3 recognize it?
The error was cause by my php version that doesn't accept return type declarations. The Rbac module is full of it.

symfony 2.3 error: autoloader find file but not class

First, sorry if my english it's not so good.
I readed a lot of questions like the one i have, but any solution works.
The question is that I'm developing a porject in Symfony 2.3, yes, i'm beginner using it...
I've created a 'Userbundle', and i want to display the info profile of an user.
When I access to the correct URL I have the famous message error:
"The autoloader expected class "Mylife\UserBundle\Entity\UserRepository" to be defined in file "D:\www\Symfony/src\Mylife\UserBundle\Entity\UserRepository.php". The file was found but the class was not in it, the class name or namespace probably has a typo."
That's my default controller code:
namespace Mylife\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mylife\UserBundle\Entity\User;
use Mylife\UserBundle\Form\Frontend\RegisterType;
class DefaultController extends Controller
{
public function profileAction(){
$user_id=1;
$em=$this->getDoctrine()->getEntityManager();
$profile=$em->getRepository('UserBundle:User')
->findProfile($user_id);
return $this->render('UserBundle:Default:profile.html.twig', array( 'profile' => $profile));
}
And my UserRepository.php code:
// src/Mylife/UserBundle/Entity/UserRepository.php
namespace Mylife\UserBundle\Entity;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function findProfile($user)
{
$em = $this->getEntityManager();
$consult= $em->createQuery('
SELECT u, nk, n
FROM UserBundle:User u
WHERE u.user= :id');
$consult->setParameter('id', $user);
return $consult->getResult();
}
}
I have the same problem when trying to use a form class in the same bundle, but i no see any error in namesapce or class name.
The project structure is:
-src
-Mylife
-UserBundle
....
-Entity
...
-User.php
-UserRepository.php
I'm going mad trying to solve the problem and reading a lot of forums and examples.
I've tryed to dissable APC, to restart Apache, erase the cache, and nothing of this worked.
Thanks a lot!!
Carlos
PD: I'm not sure why appears a piece of code at the top of the error page and why it begins in "getEntityMAnager();..." row... Why is not showing the text code before it?. Image:http://es.tinypic.com?ref=r0s8k5
IMPORTANT: When I generated the entity USer by console, I say "no" when asked to generate repository. May be this is the problem. Any suggestion now?
Thanks again
Try to add this comment in your User entity file:
/**
*
* #ORM\Entity(repositoryClass="YourProject\UserBundle\Entity\UserRepository")
*/
class User
{
...
}
or something like this:
custom repository class in symfony2
Found it!!
It's was a silly mistake. I've began the PHP repository file with
<?
...
and must be
<?php
...
Sorry at all!

Implementing GeocodableBehavior in Symfony 1.4 (using Propel)

I'm trying to implement the GeocodableBehavior on a Symfony 1.4 (with Propel 1.6) project i'm working on, but until now it's a complete failure. I've tried to search if other people but I didn't found anything, like if I was the only one having troubles with this.
So, maybe I'm missing something very very easy, but following the instructions given on the GeocodableBehavior leads to nothing but errors, and I can't figure out where's the problem.
I followed instructions for the GeocodableBehavior (here -> http://www.propelorm.org/cookbook/geocodable-behavior.html)
This seems to work as i'm getting the latitude/longitude columns created on my model. Until then, it works fine.
Where things get a little more complicated is when trying to save an object with the GeocodableBehavior, there's problems with the Geocoder class.
(Documentation here -> https://github.com/willdurand/Geocoder)
My class is Point, referring to a geolocated point, an address. When creating a Point using sf admin generator, the behavior which is supposed to use some fields (street, postal_code, country, etc) to query the GoogleMaps api, just fails to use the Geocoder class.
Fatal error: Class 'Geocoder\Geocoder' not found in /var/www/vhosts/www._________.local/lib/model/om/BasePoint.php on line 3717
I put the Geocoder class in a lib/vendor/geocoder folder, I tried to use the autoload.yml file to load it, but nothing changes...
autoload:
geocoder:
name: geocoder
path: %SF_LIB_DIR%/vendor/geocoder
recursive: on
There's something i'm missing in how to load those classes in my sf project, and i can't find what. Geocoder package has an autoload.php file but i didn't manage to "load" it successfully...
Thanks in advance.
I know it's kinda giving up on the autoloader, but you could establish a register function in /config/ProjectConfiguration.class.php. The only downside is that you will need to add a call to the function before any block that uses Geocoder.
class ProjectConfiguration extends sfProjectConfiguration
{
static protected $geocoderLoaded = false;
static public function registerGeocoder()
{
if (self::$geocoderLoaded) {
return;
}
require_once sfConfig::get('sf_lib_dir') . '/vendor/geocoder/autoload.php';
self::$geocoderLoaded = true;
}
...
}
Then just execute ProjectConfiguration::registerGeocoder(); anywhere you'd need the class. It's more annoying than getting the autoloader to work, but it's at least dependable.
Did you check your autoload cache to see it there is something related to Geocoder?
/cache/[apps_name]/dev/config/config_autoload.yml.php
/cache/project_autoload.cache
Maybe, manually add the autoload in the /config/ProjectConfiguration.class.php:
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
require_once sfConfig::get('sf_lib_dir').'/vendor/geocoder/src/autoload.php';
Using the built-in autoloader should be a working option, but you can also combine symfony's autoloader with a "PSR-0 enabled" one. Basically, this boils down to the following implementation:
public function setup()
{
// plugin stuff here
// register the new autoloader
spl_autoload_register(array($this, 'autoloadNamespace'));
}
public function autoloadNamespace($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\'))
{
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
// make sure that the path to Geocoder is correct
foreach(array(
sfConfig::get('sf_lib_dir').'/vendor/Geocoder/src' . DIRECTORY_SEPARATOR . $fileName . $className . '.php',
) as $fileName)
{
if (file_exists($fileName))
{
require $fileName;
return true;
}
}
return false;
}
With this additional autoloader, your application should be able to use Geocoder.

Why can't load data these fixtures in Symfony2 project?

I've created a fixtures class inside my bundle's DataFixtures/ORM folder. The actual class looks like this:
use Doctrine\Common\DataFixtures\FixtureInterface,
Company\ShoppingBundle\Entity\Category;
class CategoryFixtures implements FixtureInterface
{
public function load($em)
{
$category1 = new Category()->setName("category1");
$category2 = new Category()->setName("category2");
$em->persist($category1);
$em->persist($category2);
$em->flush();
}
}
I'm not using the AbstractFixture base class, since I don't need references. I've also tried specifying the fixtures path when running the doctrine:fixtures:load console command. I'm following the official docs here.
Nothing wrong with the file naming, or my configuration: I simply forgot to put
<?php
at the top of my file :-D

PHPUnit inclusion path issues

This one's got me stumped. I've been working with PHPUnit for a couple of months now, so I'm not that green...but I look forward to being pointed in the direction of the obvious mistake I'm making! The initialisation process outlined below works fine if I run the "app" from a browser - but PHPUnit is choking...can any one put me out of my misery?
I'm trying to test a homebrew MVC, for study purposes. It follows a typical ZF layout.
Here's the index page:
include './../library/SKL/Application.php';
$SKL_Application = new SKL_Application();
$SKL_Application->initialise('./../application/configs/config.ini');
Here's the application class (early days...)
include 'bootstrap.php';
class SKL_Application {
/**
* initialises the application
*/
public function initialise($file) {
$this->processBootstrap();
//purely to test PHPUnit is working as expected
return true;
}
/**
* iterates over bootstrap class and executes
* all methods prefixed with "_init"
*/
private function processBootstrap() {
$Bootstrap = new Bootstrap();
$bootstrap_methods = get_class_methods($Bootstrap);
foreach ($bootstrap_methods as $method) {
if(substr($method,0,5) == '_init'){
$bootstrap->$method();
}
}
return true;
}
}
Here's the test:
require_once dirname(__FILE__).'/../../../public/bootstrap.php';
require_once dirname(__FILE__).'/../../../library/SKL/Application.php';
class SKL_ApplicationTest extends PHPUnit_Framework_TestCase {
protected $object;
protected function setUp() {
$this->object = new SKL_Application();
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
public function testInitialise() {
$this->assertType('boolean',$this->object->initialise());
}
}
But I keep stumbling at the first hurdle!!
PHP Warning: include(bootstrap.php): failed to open stream:
No such file or directory in path\to\files\SKL\Application.php on line 9
any ideas?
Use include_once or better yet require_once instead of include to include the bootstrap.php in the Application class file. Despite being already loaded include loads it again but since it's obviously not on the include path you get the error.
Thanks to Raoul Duke for giving me a push in the right direction, here's where I got to so far
1 - add the root of the application to the include path
2 - make all inclusion paths relative to the root of the application
3 - include a file in your unit tests that performs the same function, but compensates for the relative location when it is included. I just used realpath() on the directory location of the files.
The problem I have now is that the darn thing won't see any additional files I'm trying to pass it.
So, I'm trying to test a configuration class, that will parse a variety of filetypes dynamically. The directory structure is like this:
Application_ConfigTest.php
config.ini
The first test:
public function testParseFile() {
$this->assertType('array',$this->object->parseFile('config.ini'));
}
The error:
failed to open stream: No such file or directory
WTF? It's IN the same directory as the test class...
I solved this by providing an absolute (i.e. file structure) path to the configuration file.Can anyone explain to me how PHPUnit resolves it's paths, or is it because the test class itself is included elsewhere, rendering relative paths meaningless?

Resources