|
Been using CodeIgniter 2.0 to recreate an existing project of mine from the ground up. It's an AFL (Australian Football League) tipping thing, where my fellow employees can log in and place their tips etc. I'm struggling with how to structure the project. My database looks something like this (simplified): code:
code:
I'm completely loving lost to be honest - how do I arrange all these objects so they make sense while still keeping the MVC structure intact? Edit: Should these perhaps be created as libraries? putin is a cunt fucked around with this message at 03:02 on Mar 23, 2011 |
# ¿ Mar 23, 2011 02:51 |
|
|
# ¿ May 7, 2024 12:32 |
|
Thanks for the help. I should have explained the fields a bit better, but AFL is divided up into a set number of rounds per season, so the round_id is used to determine which round any given game belongs to. In other words, yeah, it's an INT. I like the way you've simplified the database model, and I reckon I'll take that advice on board. Thanks!
|
# ¿ Mar 23, 2011 08:18 |
|
I think we broke it. Edit: nvm. You might wanna look at restricting the length of the name field however...
|
# ¿ Mar 28, 2011 05:13 |
|
Does anyone have any good resource about this Manager pattern? I'm struggling a little to get my brain to think in OOP mode, and I'd like to know more about using Managers etc so I can apply it to a particular project I'm working on. Here's what I think I would use so far, bear with me, this is gonna be long-winded. The project I'm working on is for Footy Tipping (guessing the outcome of AFL games and accumulating a score for each correct pick - at the end of the season the 'tipster' with the highest score is named the winner). For those who don't know, AFL works like this:
code:
|
# ¿ Mar 29, 2011 00:46 |
|
I'm really hoping someone can help me here because I'm completely stumped. I have a CodeIgniter site which has a controller called "tip" (it's a football tipping site). The tip controller looks like this: php:<? include_once 'app.php'; class Tip extends App { function __construct() { parent::__construct(); } function index() { echo 'test'; } function save() { echo 'test'; } } ?> quote:HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request. The bizarre part here is that exact page works if I rename the file and the class to something else and try that instead, but if I try to call it 'tip' it doesn't work. I even had that specific controller, named tip, working perfectly before and it had a lot worse code in it than what is there now. At some point the whole thing fell over so I dumbed the code down to the bare essentials for the sake of debugging, but no luck - even with the code so dumbed down it doesn't work with that specific name.
|
# ¿ Mar 22, 2012 13:47 |
|
rt4 posted:Does "app" work? Yeah sorry, forgot to mention that all of other functioning controllers start the same way. Also this specific file works fine if I simply change its name and nothing else, so I don't think app is causing it. It's making my head hurt.
|
# ¿ Mar 22, 2012 14:02 |
|
Oh sweet jesus I'm sorry guys, I found the problem. I had another class being created elsewhere called 'Tip'... ugh! I think this is evidence that I've been up too long.
|
# ¿ Mar 22, 2012 14:18 |
|
Knyteguy posted:Meh, I just changed everything to get and it worked. . I'm not very worried about pretty since it's a non-public page, so it works I suppose. It's not just about pretty - it's about common sense. If you're submitting something to enter it into a database you should be using POST. What Hammerite suggested will probably fix your issue - did you try it? Also, as mentioned you need to either use the active record stuff (http://codeigniter.com/user_guide/database/active_record.html) built into Code Igniter, use query binding or manually sanitise those inputs, $this->input->post() won't sanitise it for use in queries.
|
# ¿ Mar 28, 2012 08:23 |
|
I have a bit of a best practice question. I'm writing a model that will obviously have multiple ways of fetching the same data (Users for example can be fetched byId, byUsername, byEmail etc). Obviously this means each of those methods will need to fetch the same database fields, package them into the User class and then return that class instance. This opens up potential for lots of code repetition. I'm not explaining myself very well, I can tell already, so I'll give an example: php:<? class User { ... public static function byId($id) { $CI = get_instance(); $CI->db->select('us.user_id, us.user_name, us.user_email'); $CI->db->where('us.user_id', $id); $r = $CI->db->get('user us'); return new User($r->result()); } public static function byEmail($email) { $CI = get_instance(); $CI->db->select('us.user_id, us.user_name, us.user_email'); $CI->db->where('us.user_email', $email); $r = $CI->db->get('user us'); return new User($r->result()); } ... } ?> (By the way, I'm using CodeIgniter if you hadn't guessed). Edit: Also, bonus question! I have a User class wherein one of the properties represents the user's favourite AFL team. This property is called team and holds a Team object. Now, every time I want to get a User out of the database, I'd like to get their favourite team as well, so I can create the Team object and set it against the 'team' property for that User. Obviously I will also have a Team model and there will be other models as well that will need to have a "Team" as one or more of the class properties. Obviously I can just include the team details in the query, get it all out and once and set it all up how I want it. But say a few months down the track I want to add an extra field to the Team table. Now I have a hundred different places where I'm fetching Team details that I need to update to also fetch the new field. How do I prevent this being an issue? Help! putin is a cunt fucked around with this message at 11:33 on Jun 5, 2012 |
# ¿ Jun 5, 2012 11:25 |
|
musclecoder posted:You could use the __call() magic method, something along the lines of: Unfortunately I'm not sure if this will solve the problem as I have some methods that will need to search on two criteria - byUsernameAndPassword() for example. Then I have queries that will be much more complex. Could I perhaps use a switch statement to build the appropriate 'where' statement depending on which method is run? For example: php:<? class User { public function __call($method, $argv) { $db->select(_FIELDS_GO_HERE_); switch ($method) { case 'byUsernameAndPassword': $db->where('us.user_name', $argv[0]); $db->where('us.user_pass', $argv[1]); break; case 'byId': $db->where('us.user_id', $argv[0]); break; } $db->get('user us'); // etc etc } } ?> putin is a cunt fucked around with this message at 13:31 on Jun 5, 2012 |
# ¿ Jun 5, 2012 13:24 |
|
Sab669 posted:Can't you just do something like SQL code:
Edit: beaten! putin is a cunt fucked around with this message at 16:03 on Jun 5, 2012 |
# ¿ Jun 5, 2012 15:59 |
|
fletcher posted:I wouldn't do it like that. I would go with multiple constructors: Hmm I see - the thing is, I want it to work more like musclecoder suggested and have static methods I can use to return User instances that match the parameters. The only real problem I'm having with that is where on earth do I put the list of field names so I'm not having to retype it every time? Can I access properties statically (does that even make sense)?
|
# ¿ Jun 6, 2012 00:08 |
|
barbarianbob posted:With codeigniter you can add this as the top of the class. (This is more like your original approach) If I can keep the select list in a static property then maybe that will solve my problem. Thanks for the tip, I'll give it a go. As a disclaimer for the above - I have no formal education in OOP, everything I know has just been picked up along the way from using C++ and Java. That means I'm happy for people to point out any mistakes I'm making in terminology because some of the correct terms for things I'm not exactly clear on. revmoo posted:Didn't they change to Model from CI_Model in 2.x? putin is a cunt fucked around with this message at 01:43 on Jun 6, 2012 |
# ¿ Jun 6, 2012 01:40 |
|
PHP's documentation is a weird thing - it's frustrating and tedious to find what you want, until you eventually learn their 'way' of doing things and then it becomes some of the simplest and comprehensive documentation I've ever read.
|
# ¿ Jun 6, 2012 03:55 |
|
DarkLotus posted:Why the AND? It seems redundant. Also, that snippet of code makes my head hurt. I'm guessing he wants to check both conditions? What's wrong with that? As for the problem at hand, try this perhaps? PHP code:
PHP code:
On a different note - does anyone know if CodeIgniter uses PDO for its DB stuff? putin is a cunt fucked around with this message at 00:52 on Jun 25, 2012 |
# ¿ Jun 25, 2012 00:49 |
|
revmoo posted:If it does, it's added a pretty hefty abstraction layer, because CI has a lot of db functions. So... "no"? Or "I don't know"?
|
# ¿ Jun 25, 2012 06:36 |
|
Scaramouche posted:Yeah, I figured that part out right away but it just felt really strange. It feels strange that a valid result for a function could be 0 (found at first position), !true (not found), or >0 (found). Do you mean because you're expecting it to be -1 if it's not found?
|
# ¿ Jul 20, 2012 06:53 |
|
Scaramouche posted:I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons. I agree completely - I think that's why I was a bit confused about what you were saying. Even though the return type is mixed, I think it's okay in this instance because it does make sense to return a FALSE. The problem lies in the fact that 0 will still compare as false, I guess it pays to always use the strict comparisons - sucks for people just getting into the language though as it's a frequent gotcha that will continuously crop up.
|
# ¿ Jul 23, 2012 00:55 |
|
I have learnt more about user authentication in the last two pages than I probably have over my entire time spent learning PHP. Thanks guys! On a separate note I have a problem that I'm not sure how to approach - I have a web site that uses "some data" to do "some stuff". I get the data via a web service that updates every 10 minutes, so theoretically if I access the web service every ten minutes I'll get the most recent data. I need to then take that data and insert some of it into a database. How do I do this? Is PHP even the appropriate tool for this? The nature of the web site means that I won't need to have up-to-date data if no one is on the web site (ie. I could access the web site and update the data on each page load) but I don't want to have it occur on every page load since it will create quite a bit of extra load time for every page. So yeah, crux of the issue - I need a script to run every 10 minutes and I need to know whether PHP is the right way to approach this or is there something else more suitable?
|
# ¿ Aug 17, 2012 01:08 |
|
Hammerite posted:PHP could be used for this. Any web scripting language could. Python, Perl or Ruby would also be usable. You schedule a cron job to run your script (whatever type of script) every 10 minutes. IT Guy posted:Yeah, PHP can definietly do this job. Thanks guys, I had considered a cron job but I was under the impression my host doesn't allow cron jobs to run quite so frequently as 'every 10 minutes'. I had another look and it turns out they do! So I'll go with that approach - thanks!
|
# ¿ Aug 17, 2012 02:05 |
|
Ursine Asylum posted:Depending on the site traffic, how touchy your host is about cron jobs, and how long the site update function takes, you could always just implement a poor man's caching system, serialize some data to a file, and update the file on page load if it's >X minutes old. This is good advice, thanks - I now have more than one option!
|
# ¿ Aug 17, 2012 04:51 |
|
McGlockenshire posted:All ORMs suck. It's just a matter of where you want the suck to live. As a PHP newbie, I want to make sure I'm parsing this properly: are you saying that "All ORMs suck" therefore you shouldn't use one? Or "All ORMs suck" but it's a necessary evil so pick the best of the crappy offerings? One of my biggest problems at the moment is getting the hang of the overall structure of a large PHP project and managing the database connection and queries etc is a big of a pain for me right now, so this topic is particularly interesting to me. putin is a cunt fucked around with this message at 00:08 on Aug 22, 2012 |
# ¿ Aug 22, 2012 00:02 |
|
Doh004 posted:As a PHP newbie, may I ask why you're working on an large project? I'm not sure how that's relevant, but it's my own project in my own time, I'm doing it to learn - is that okay?
|
# ¿ Aug 22, 2012 02:59 |
|
Doh004 posted:No it's not okay! (of course it is). Probably a more accurate way to put it is that I'm familiar with PHP, but new to large projects.
|
# ¿ Aug 28, 2012 00:20 |
|
Sab669 posted:If I just throw in a var_dump($result) in that function, I get "bool(false)" as my output? What the heck does that even mean? And print_r yields noting. If you're not getting an error in the query then it means your query hasn't returned any rows (fetch() returns bool(false) on failure). Try dumping the values of $email and $password immediately before they're used to check that they have been set correctly.
|
# ¿ Aug 30, 2012 00:24 |
|
IT Guy posted:Did you copy/paste an already hashed password and then rehashing it? I'll put money on this. And I don't mean that in a cruel way, I mean that in a "I've made this same mistake myself" way. Alternatively, if you copy-pasted is it possible a trailing space was picked up somewhere along the way?
|
# ¿ Aug 30, 2012 00:40 |
|
Got a bit of a tricky one - I'm working on a personal project that is game-like and I'm wanting to include some sort of achievement system. The achievements can be user-activated (ie. the user performs an action and that action results in them getting an achievement) or they may be passive (ie. a change is made in the database - scores being calculated for example - that results in the user getting an achievement). There will be data coming in from an external source that could result in some users receiving achievements. I'm wondering what is the best way to approach this? I could obviously have a general "check for achievements" function that gets run as a cronjob every 10 minutes or so and checks whether there are any achievements to award, but this won't provide a very immediate result. It's like I need something that can sit passively and "wait" for certain conditions to be met and then award the appropriate achievements. Am I making any sense at all here? putin is a cunt fucked around with this message at 04:15 on Aug 30, 2012 |
# ¿ Aug 30, 2012 04:08 |
|
McGlockenshire posted:You're looking for a job queue. That looks along the right lines, but having not done this before it seems complicated as heck. It'll take a bit of reading but I think you've nailed the right solution for me, thanks! Edit: I don't suppose you have any resources that might help me get started in this? putin is a cunt fucked around with this message at 07:29 on Aug 30, 2012 |
# ¿ Aug 30, 2012 07:25 |
|
McGlockenshire posted:The other year, there was some group doing some sort of PHP advent calendar thing, and it was awesome and useful and it had hands down the best intro to using the two together ever. I also forgot to bookmark it and can't find it now. Sigh. Thanks for that - searching phpadvent and Gearman I was able to find a few decent articles, this one sounds like it may be the one you were talking about? http://phpadvent.org/2009/daemonize-your-php-by-sean-coates
|
# ¿ Aug 31, 2012 04:52 |
|
IT Guy posted:Anyone know of any good books that focus on single page web application design using PHP and JS? I would also be keenly interested in this!
|
# ¿ Sep 9, 2012 23:49 |
|
Deus Rex posted:we're using FuelPHP which has its warts, but it meant to be a 'cleaner' version of CI or something. Anyway, I've poked around for fun at Laravel and it seems really very promising. Thanks for this - I hadn't heard of Laravel and it looks quite interesting. Edit: So I've been looking into Laravel, playing around with it a bit, and I'm struggling to get the hang of the Eloquent ORM. In my schema I have a USERS table and a TEAMS table, each user record has a TEAM_ID (which may be null) representing the football team they support. I must be misunderstanding something, because I would call this a one to many (one team can be linked to any number of users, but each user will be linked to just one team). According to the docs I would reflect a one-to-many relationship in the code by saying: PHP code:
I'm clearly misunderstanding something fairly fundamental, someone please help! putin is a cunt fucked around with this message at 16:45 on Sep 26, 2012 |
# ¿ Sep 26, 2012 03:41 |
|
PlesantDilemma posted:Yeah there are a lot of frameworks out there. Which one has built in models to manage users? I'd like something that already has the sign up/login/recover password thing already made and I can just extend from. I've been using code igniter because I have experience with it from a school project, but it doesn't come with user stuff already. Laravel comes with the Auth stuff built-in, it seems like it's going to be the next big thing in my opinion as it's incredibly well made and actually makes good use of the improved OO in PHP 5.3 Edit: forgot to mention, also has great documentation.
|
# ¿ Sep 27, 2012 06:52 |
|
Eloquent ORM is used in the Laravel framework and it's basically the only ORM I have ever used that I've not just "not-hated" but actually loved. It actually handles all the relationships (one-to-one, one-to-many, many-to-many) without issue which is great when you want to say, remove a post and also delete all the entries from your compound tables etc.
putin is a cunt fucked around with this message at 01:45 on Oct 15, 2012 |
# ¿ Oct 15, 2012 01:29 |
|
fuf posted:I'm looping through wordpress posts and want a new line after every three posts. Normally you would handle presentation issues like this in CSS, is there a functional reason for needing to place a linebreak on every third post or is it purely for looks? If it's purely for looks, use CSS instead.
|
# ¿ Oct 21, 2012 23:15 |
|
fuf posted:Yeah it's purely for looks, but I don't know how I would do it with just CSS. I need PHP to tell me whether a post should be on the start of a new row so I know when to close the last row and start a new one. I guess I could have written the CSS better so that each row didn't have its own container, but that's the way it's laid out... Biowarfare posted:div:nth-child(Xn+Y) {} or something? Yep, you'd say: code:
|
# ¿ Oct 26, 2012 00:57 |
|
For those of you wondering which framework you should be using, if you are able to guarantee your server will have PHP 5.3 then you should almost certainly be using Laravel - it's super amazing.
|
# ¿ Nov 10, 2012 09:49 |
|
IT Guy posted:You guys using Laravel, when interacting with the database, what do you prefer (raw query, fluent, eloquent)? Eloquent is the bee's knees. It took me a little bit of work and a lot of reading to fully understand it, but it's worth it. If you ever get completely stuck you can browse the actual Laravel code on their web site as well.
|
# ¿ Jan 11, 2013 05:59 |
|
I have a question around testing that you lot may be able to help me with. I'm working on a footy tipping system that will use dates quite a bit to control things. For example, I'll want it to understand the concept of the 'current' football round. I'll also want it to know when a game has started and ended, that sort of thing. This is fine logically, it's easy enough to write this stuff but how would you guys go about testing it? For example, maybe I want to make sure notification emails are sent out at the correct time, or that games are locked down from tipping once they've begun - how would I test this? I could manually change the date/time of my dev environment but that's a lot of stuffing around.
|
# ¿ Feb 4, 2014 00:24 |
|
spacebard posted:Hopefully your classes are loosely-coupled enough so that you could create a unit test based off of PHPUnit_Frameworke_TestCase. Then you should have high confidence that if you assert that your isLocked method or whatever returns true or false. Sorry but I've not used that before - that will allow me to define an arbitrary point in time and have my app behave as though it is currently that time?
|
# ¿ Feb 4, 2014 01:39 |
|
|
# ¿ May 7, 2024 12:32 |
|
Blinkz0rz posted:I think what he's suggesting is that you should make your Match class's isLocked method accept a datetime rather than assume the current output of date(). That way you can arbitrarily change whatever time you want to use to test whether a match is locked. Ah okay, thank you. I did wonder if that's what he was getting at. I was hoping to avoid that but it makes sense so I think I'll go for that approach. Thanks both of you.
|
# ¿ Feb 4, 2014 03:49 |